From efb9ed0f86646947756e00c67b5111e2fd80b21f Mon Sep 17 00:00:00 2001 From: Martin Prokoph Date: Mon, 29 Jul 2024 13:05:46 +0200 Subject: [PATCH] Let there be light - Working lamps and double lamps on Polymer --- .../motschen/decorative/block/DoubleLamp.java | 14 +++++ .../motschen/decorative/block/Lamp.java | 20 +++++-- .../decorative/polymer/PolymerSupport.java | 4 ++ .../model/ItemDisplayDigitalClockModel.java | 6 +- .../model/ItemDisplayDoubleLampModel.java | 60 +++++++++++++++++++ .../polymer/model/ItemDisplayLampModel.java | 56 +++++++++++++++++ .../motschen/decorative/util/ColorUtil.java | 2 + 7 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDoubleLampModel.java create mode 100644 src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayLampModel.java diff --git a/src/main/java/eu/midnightdust/motschen/decorative/block/DoubleLamp.java b/src/main/java/eu/midnightdust/motschen/decorative/block/DoubleLamp.java index 366a86e..cc40b2f 100755 --- a/src/main/java/eu/midnightdust/motschen/decorative/block/DoubleLamp.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/block/DoubleLamp.java @@ -1,6 +1,8 @@ package eu.midnightdust.motschen.decorative.block; +import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayDoubleLampModel; import eu.pb4.factorytools.api.block.FactoryBlock; +import eu.pb4.polymer.virtualentity.api.ElementHolder; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -12,6 +14,8 @@ import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; @@ -28,6 +32,7 @@ import net.minecraft.world.BlockView; import net.minecraft.world.World; import net.minecraft.world.WorldAccess; import net.minecraft.world.WorldView; +import org.jetbrains.annotations.Nullable; public class DoubleLamp extends Block implements FactoryBlock { private static final VoxelShape SHAPE_TOP; @@ -103,4 +108,13 @@ public class DoubleLamp extends Block implements FactoryBlock { public BlockState getPolymerBlockState(BlockState state) { return Blocks.BARRIER.getDefaultState(); } + @Override + public BlockState getPolymerBreakEventBlockState(BlockState state, ServerPlayerEntity player) { + return Blocks.WHITE_WOOL.getDefaultState(); + } + + @Override + public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) { + return new ItemDisplayDoubleLampModel(initialBlockState); + } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/block/Lamp.java b/src/main/java/eu/midnightdust/motschen/decorative/block/Lamp.java index 0daafbe..15e45f5 100755 --- a/src/main/java/eu/midnightdust/motschen/decorative/block/Lamp.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/block/Lamp.java @@ -1,6 +1,8 @@ package eu.midnightdust.motschen.decorative.block; +import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayLampModel; import eu.pb4.factorytools.api.block.FactoryBlock; +import eu.pb4.polymer.virtualentity.api.ElementHolder; import net.minecraft.block.AbstractBlock; import net.minecraft.block.Block; import net.minecraft.block.BlockState; @@ -9,18 +11,20 @@ import net.minecraft.block.RedstoneLampBlock; import net.minecraft.block.ShapeContext; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemPlacementContext; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.util.ActionResult; -import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.shape.VoxelShape; import net.minecraft.world.BlockView; import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; public class Lamp extends Block implements FactoryBlock { private static final VoxelShape SHAPE; @@ -41,7 +45,7 @@ public class Lamp extends Block implements FactoryBlock { @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) { - world.setBlockState(pos, state.with(LIT, Boolean.valueOf(!state.get(LIT)))); + world.setBlockState(pos, state.with(LIT, !state.get(LIT))); world.playSound(player, pos, SoundEvents.BLOCK_STONE_BUTTON_CLICK_ON, SoundCategory.BLOCKS, 0.2f, 0.5f); return ActionResult.SUCCESS; } @@ -50,8 +54,7 @@ public class Lamp extends Block implements FactoryBlock { return SHAPE; } static { - VoxelShape shape = createCuboidShape(4, 0, 4, 12, 10, 12); - SHAPE = shape; + SHAPE = createCuboidShape(4, 0, 4, 12, 10, 12); } // Polymer @@ -59,4 +62,13 @@ public class Lamp extends Block implements FactoryBlock { public BlockState getPolymerBlockState(BlockState state) { return Blocks.BARRIER.getDefaultState(); } + @Override + public BlockState getPolymerBreakEventBlockState(BlockState state, ServerPlayerEntity player) { + return Blocks.WHITE_WOOL.getDefaultState(); + } + + @Override + public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) { + return new ItemDisplayLampModel(initialBlockState); + } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/polymer/PolymerSupport.java b/src/main/java/eu/midnightdust/motschen/decorative/polymer/PolymerSupport.java index b337b9b..9e24df5 100644 --- a/src/main/java/eu/midnightdust/motschen/decorative/polymer/PolymerSupport.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/polymer/PolymerSupport.java @@ -5,6 +5,8 @@ import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayCeilingFanMo import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayChristmasLightsModel; import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayChristmasTreeModel; import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayDigitalClockModel; +import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayDoubleLampModel; +import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayLampModel; public class PolymerSupport { public static void init() { @@ -13,5 +15,7 @@ public class PolymerSupport { ItemDisplayChristmasLightsModel.initModels(); ItemDisplayChristmasTreeModel.initModels(); ItemDisplayDigitalClockModel.initModels(); + ItemDisplayDoubleLampModel.initModels(); + ItemDisplayLampModel.initModels(); } } diff --git a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDigitalClockModel.java b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDigitalClockModel.java index cfcbef6..dd85f23 100644 --- a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDigitalClockModel.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDigitalClockModel.java @@ -28,12 +28,12 @@ import static eu.midnightdust.motschen.decorative.util.TimeUtil.getTime; public class ItemDisplayDigitalClockModel extends BlockModel { private final ItemDisplayElement main; private final TextDisplayElement text; - private static final Map models = new HashMap<>(); + private static final Map MODELS = new HashMap<>(); public static void initModels() { for (int i = 0; i < ColorUtil.VanillaColor.length(); i++) { String color = ColorUtil.VanillaColor.byNumber(i).getName(); - models.put(color, BaseItemProvider.requestModel(id("block/"+color+"_digital_clock"))); + MODELS.put(color, BaseItemProvider.requestModel(id("block/"+color+"_digital_clock"))); } } @@ -73,7 +73,7 @@ public class ItemDisplayDigitalClockModel extends BlockModel { this.text.setText(Text.of(getTime())); } public ItemStack getModel(BlockState state) { - return models.get(ColorUtil.VanillaColor.fromBlockName(state.getBlock().getTranslationKey()).getName()); + return MODELS.get(ColorUtil.VanillaColor.fromBlockName(state.getBlock().getTranslationKey()).getName()); } public float getRotation(BlockState state) { return state.get(DigitalClock.FACING).getHorizontal() * -90; diff --git a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDoubleLampModel.java b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDoubleLampModel.java new file mode 100644 index 0000000..d293c4b --- /dev/null +++ b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayDoubleLampModel.java @@ -0,0 +1,60 @@ +package eu.midnightdust.motschen.decorative.polymer.model; + +import eu.midnightdust.motschen.decorative.block.DoubleLamp; +import eu.midnightdust.motschen.decorative.config.DecorativeConfig; +import eu.midnightdust.motschen.decorative.util.ColorUtil; +import eu.pb4.factorytools.api.resourcepack.BaseItemProvider; +import eu.pb4.factorytools.api.virtualentity.BlockModel; +import eu.pb4.factorytools.api.virtualentity.ItemDisplayElementUtil; +import eu.pb4.polymer.virtualentity.api.attachment.BlockAwareAttachment; +import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; +import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement; +import net.minecraft.block.BlockState; +import net.minecraft.block.enums.DoubleBlockHalf; +import net.minecraft.item.ItemStack; +import org.joml.Vector3f; + +import java.util.HashMap; +import java.util.Map; + +import static eu.midnightdust.motschen.decorative.DecorativeMain.id; + +public class ItemDisplayDoubleLampModel extends BlockModel { + private final ItemDisplayElement main; + private static ItemStack MODEL_BOTTOM; + private static final Map MODELS_OFF = new HashMap<>(); + private static final Map MODELS_ON = new HashMap<>(); + + public static void initModels() { + MODEL_BOTTOM = BaseItemProvider.requestModel(id("block/double_lamp_bottom")); + for (int i = 0; i < ColorUtil.VanillaColor.length(); i++) { + String color = ColorUtil.VanillaColor.byNumber(i).getName(); + MODELS_OFF.put(color, BaseItemProvider.requestModel(id("block/"+color+"_double_lamp_off_top"))); + MODELS_ON.put(color, BaseItemProvider.requestModel(id("block/"+color+"_double_lamp_on_top"))); + } + } + + public ItemDisplayDoubleLampModel(BlockState state) { + this.main = ItemDisplayElementUtil.createSimple(getModel(state)); + this.main.setDisplaySize(1, 1); + this.main.setScale(new Vector3f(2)); + this.main.setViewRange(DecorativeConfig.viewDistance / 100f); + this.addElement(this.main); + } + + @Override + public void notifyUpdate(HolderAttachment.UpdateType updateType) { + if (updateType == BlockAwareAttachment.BLOCK_STATE_UPDATE) { + var state = this.blockState(); + this.main.setItem(getModel(state)); + + this.tick(); + } + } + public ItemStack getModel(BlockState state) { + if (state.get(DoubleLamp.HALF) == DoubleBlockHalf.LOWER) return MODEL_BOTTOM; + String color = ColorUtil.VanillaColor.fromBlockName(state.getBlock().getTranslationKey()).getName(); + return state.get(DoubleLamp.LIT) ? MODELS_ON.get(color) : MODELS_OFF.get(color); + } +} + diff --git a/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayLampModel.java b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayLampModel.java new file mode 100644 index 0000000..066550c --- /dev/null +++ b/src/main/java/eu/midnightdust/motschen/decorative/polymer/model/ItemDisplayLampModel.java @@ -0,0 +1,56 @@ +package eu.midnightdust.motschen.decorative.polymer.model; + +import eu.midnightdust.motschen.decorative.block.DoubleLamp; +import eu.midnightdust.motschen.decorative.config.DecorativeConfig; +import eu.midnightdust.motschen.decorative.util.ColorUtil; +import eu.pb4.factorytools.api.resourcepack.BaseItemProvider; +import eu.pb4.factorytools.api.virtualentity.BlockModel; +import eu.pb4.factorytools.api.virtualentity.ItemDisplayElementUtil; +import eu.pb4.polymer.virtualentity.api.attachment.BlockAwareAttachment; +import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; +import eu.pb4.polymer.virtualentity.api.elements.ItemDisplayElement; +import net.minecraft.block.BlockState; +import net.minecraft.item.ItemStack; +import org.joml.Vector3f; + +import java.util.HashMap; +import java.util.Map; + +import static eu.midnightdust.motschen.decorative.DecorativeMain.id; + +public class ItemDisplayLampModel extends BlockModel { + private final ItemDisplayElement main; + private static final Map MODELS_OFF = new HashMap<>(); + private static final Map MODELS_ON = new HashMap<>(); + + public static void initModels() { + for (int i = 0; i < ColorUtil.VanillaColor.length(); i++) { + String color = ColorUtil.VanillaColor.byNumber(i).getName(); + MODELS_OFF.put(color, BaseItemProvider.requestModel(id("block/"+color+"_lamp_off"))); + MODELS_ON.put(color, BaseItemProvider.requestModel(id("block/"+color+"_lamp_on"))); + } + } + + public ItemDisplayLampModel(BlockState state) { + this.main = ItemDisplayElementUtil.createSimple(getModel(state)); + this.main.setDisplaySize(1, 1); + this.main.setScale(new Vector3f(2)); + this.main.setViewRange(DecorativeConfig.viewDistance / 100f); + this.addElement(this.main); + } + + @Override + public void notifyUpdate(HolderAttachment.UpdateType updateType) { + if (updateType == BlockAwareAttachment.BLOCK_STATE_UPDATE) { + var state = this.blockState(); + this.main.setItem(getModel(state)); + + this.tick(); + } + } + public ItemStack getModel(BlockState state) { + String color = ColorUtil.VanillaColor.fromBlockName(state.getBlock().getTranslationKey()).getName(); + return state.get(DoubleLamp.LIT) ? MODELS_ON.get(color) : MODELS_OFF.get(color); + } +} + diff --git a/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java b/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java index f921463..911b1ac 100644 --- a/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java +++ b/src/main/java/eu/midnightdust/motschen/decorative/util/ColorUtil.java @@ -46,6 +46,8 @@ public class ColorUtil { return Arrays.stream(vals).filter(color -> name .replace("block.decorative.", "") .replace("_digital_clock", "") + .replace("_double_lamp", "") + .replace("_lamp", "") .equals(color.getName())).findFirst().orElse(VanillaColor.BLACK); } }