Let there be light

- Working lamps and double lamps on Polymer
This commit is contained in:
Martin Prokoph
2024-07-29 13:05:46 +02:00
parent 0d65fa8cb9
commit efb9ed0f86
7 changed files with 155 additions and 7 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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<String, ItemStack> models = new HashMap<>();
private static final Map<String, ItemStack> 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;

View File

@@ -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<String, ItemStack> MODELS_OFF = new HashMap<>();
private static final Map<String, ItemStack> 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);
}
}

View File

@@ -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<String, ItemStack> MODELS_OFF = new HashMap<>();
private static final Map<String, ItemStack> 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);
}
}

View File

@@ -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);
}
}