Watch out for the signs

- Road signs now work with Polymer
This commit is contained in:
Martin Prokoph
2024-07-29 14:24:16 +02:00
parent 72b0cf0095
commit 0a5636846c
2 changed files with 62 additions and 0 deletions

View File

@@ -1,7 +1,10 @@
package eu.midnightdust.motschen.decorative.block;
import com.mojang.serialization.MapCodec;
import eu.midnightdust.motschen.decorative.polymer.model.ItemDisplayDirectionalModel;
import eu.midnightdust.motschen.decorative.util.ColorUtil;
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,6 +12,8 @@ import net.minecraft.block.Blocks;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.block.ShapeContext;
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.state.StateManager;
import net.minecraft.util.math.BlockPos;
@@ -17,6 +22,11 @@ import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import static eu.midnightdust.motschen.decorative.DecorativeMain.id;
public class Sign extends HorizontalFacingBlock implements FactoryBlock {
private static final VoxelShape NORTH_SHAPE;
@@ -83,4 +93,44 @@ public class Sign extends HorizontalFacingBlock implements FactoryBlock {
public BlockState getPolymerBlockState(BlockState state) {
return Blocks.BARRIER.getDefaultState();
}
@Override
public BlockState getPolymerBreakEventBlockState(BlockState state, ServerPlayerEntity player) {
return Blocks.IRON_BLOCK.getDefaultState();
}
@Override
public @Nullable ElementHolder createElementHolder(ServerWorld world, BlockPos pos, BlockState initialBlockState) {
return new ItemDisplayDirectionalModel(initialBlockState);
}
public enum Type {
EMPTY("empty_sign"), STOP("stop_sign"),
FIVE("five_sign"), TEN("ten_sign"), TWENTY("twenty_sign"),
THIRTY("thirty_sign"), FORTY("forty_sign"), FIFTY("fifty_sign"),
SIXTY("sixty_sign"), SEVENTY("seventy_sign"), EIGHTY("eighty_sign"),
NINETY("ninety_sign"), ONEHUNDRED("onehundred_sign"), ONEHUNDREDTEN("onehundredten_sign");
private final String name;
private static final Sign.Type[] vals = values();
Type(String name) {
this.name = name;
}
public static Sign.Type byNumber(int id) {
return vals[id];
}
public static int length() {
return vals.length;
}
public String getName() {
return name;
}
public static Sign.Type fromBlockName(String name) {
return Arrays.stream(vals).filter(color -> name
.replace("block.decorative.", "")
.equals(color.getName())).findFirst().orElse(Type.STOP);
}
}
}

View File

@@ -4,7 +4,9 @@ import eu.midnightdust.motschen.decorative.block.FireHydrant;
import eu.midnightdust.motschen.decorative.block.Guardrail;
import eu.midnightdust.motschen.decorative.block.PoolSprinkler;
import eu.midnightdust.motschen.decorative.block.ShowerHead;
import eu.midnightdust.motschen.decorative.block.Sign;
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;
@@ -16,6 +18,9 @@ import net.minecraft.item.ItemStack;
import net.minecraft.util.math.RotationAxis;
import org.joml.Vector3f;
import java.util.HashMap;
import java.util.Map;
import static eu.midnightdust.motschen.decorative.DecorativeMain.id;
public class ItemDisplayDirectionalModel extends BlockModel {
@@ -24,12 +29,17 @@ public class ItemDisplayDirectionalModel extends BlockModel {
public static ItemStack GUARDRAIL;
public static ItemStack POOL_SPRINKLER;
public static ItemStack SHOWER_HEAD;
private static final Map<String, ItemStack> SIGNS = new HashMap<>();
public static void initModels() {
FIRE_HYDRANT = BaseItemProvider.requestModel(id("block/fire_hydrant"));
GUARDRAIL = BaseItemProvider.requestModel(id("block/guardrail"));
POOL_SPRINKLER = BaseItemProvider.requestModel(id("block/pool_sprinkler"));
SHOWER_HEAD = BaseItemProvider.requestModel(id("block/shower_head"));
for (int i = 0; i < Sign.Type.length(); i++) {
String color = Sign.Type.byNumber(i).getName();
SIGNS.put(color, BaseItemProvider.requestModel(id("block/"+color)));
}
}
public ItemDisplayDirectionalModel(BlockState state) {
@@ -54,12 +64,14 @@ public class ItemDisplayDirectionalModel extends BlockModel {
if (state.getBlock() instanceof FireHydrant) return FIRE_HYDRANT;
else if (state.getBlock() instanceof PoolSprinkler) return POOL_SPRINKLER;
else if (state.getBlock() instanceof ShowerHead) return SHOWER_HEAD;
else if (state.getBlock() instanceof Sign) return SIGNS.get(Sign.Type.fromBlockName(state.getBlock().getTranslationKey()).getName());
else return GUARDRAIL;
}
public float getRotation(BlockState state) {
if (state.getBlock() instanceof FireHydrant) return state.get(FireHydrant.FACING).getHorizontal() * -90;
else if (state.getBlock() instanceof PoolSprinkler) return state.get(PoolSprinkler.FACING).getHorizontal() * -90;
else if (state.getBlock() instanceof ShowerHead) return state.get(ShowerHead.FACING).getHorizontal() * -90 + 90;
else if (state.getBlock() instanceof Sign) return state.get(PoolSprinkler.FACING).getHorizontal() * -90 + 180;
else return state.get(Guardrail.FACING).getHorizontal() * -90;
}
}