MrGise

Untitled

Dec 13th, 2025
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.80 KB | Source Code | 0 0
  1. package net.MrGise.mmm.block;
  2.  
  3. import com.google.common.collect.BiMap;
  4. import com.google.common.collect.HashBiMap;
  5. import net.MrGise.mmm.registry.back.ModTags;
  6. import net.minecraft.core.BlockPos;
  7. import net.minecraft.core.Direction;
  8. import net.minecraft.resources.ResourceLocation;
  9. import net.minecraft.tags.ItemTags;
  10. import net.minecraft.util.StringRepresentable;
  11. import net.minecraft.world.InteractionHand;
  12. import net.minecraft.world.InteractionResult;
  13. import net.minecraft.world.entity.player.Player;
  14. import net.minecraft.world.item.Item;
  15. import net.minecraft.world.item.ItemStack;
  16. import net.minecraft.world.item.Items;
  17. import net.minecraft.world.item.context.BlockPlaceContext;
  18. import net.minecraft.world.level.ItemLike;
  19. import net.minecraft.world.level.Level;
  20. import net.minecraft.world.level.block.Block;
  21. import net.minecraft.world.level.block.HorizontalDirectionalBlock;
  22. import net.minecraft.world.level.block.state.BlockState;
  23. import net.minecraft.world.level.block.state.StateDefinition;
  24. import net.minecraft.world.level.block.state.properties.BooleanProperty;
  25. import net.minecraft.world.level.block.state.properties.EnumProperty;
  26. import net.minecraft.world.phys.BlockHitResult;
  27. import net.minecraftforge.registries.ForgeRegistries;
  28. import org.jetbrains.annotations.Nullable;
  29.  
  30. import java.util.HashMap;
  31. import java.util.Map;
  32.  
  33. public class HanukiyaBlock extends HorizontalDirectionalBlock {
  34.     public enum Candle implements StringRepresentable {
  35.         NONE,
  36.         WHITE, LIGHT_GRAY,
  37.         GRAY, BLACK,
  38.         BROWN, RED,
  39.         ORANGE, YELLOW,
  40.         LIME, GREEN,
  41.         CYAN, LIGHT_BLUE,
  42.         BLUE, PURPLE,
  43.         MAGENTA, PINK,
  44.         EMPTY;
  45.  
  46.         public Item asItem() {
  47.             if (this == EMPTY) return Items.AIR;
  48.  
  49.             return ForgeRegistries.ITEMS.getValue(
  50.                 this.isLit()
  51.                         ? ResourceLocation.fromNamespaceAndPath("mmm", this.getSerializedName() + "_candle_lit")
  52.                         : ResourceLocation.withDefaultNamespace(this.getSerializedName() + "_candle")
  53.             );
  54.         }
  55.         public Item unlitItem() {
  56.             if (this == EMPTY) return Items.AIR;
  57.  
  58.             return ForgeRegistries.ITEMS.getValue(
  59.                 ResourceLocation.withDefaultNamespace(this.getSerializedName() + "_candle")
  60.             );
  61.         }
  62.         public boolean isLit() {
  63.             return this.name().endsWith("_LIT");
  64.         }
  65.  
  66.         @Override
  67.         public String getSerializedName() {
  68.             return name().toLowerCase();
  69.         }
  70.     }
  71.  
  72.     public void convertToState(BlockState state, ItemLike item, EnumProperty<Candle> property) {
  73.         for (Candle candle : Candle.values()) {
  74.             if (candle == Candle.EMPTY) continue;
  75.  
  76.             if (candle.unlitItem() == item.asItem()) {
  77.                 state = state.setValue(property, candle);
  78.                 state = state.setValue(litToProperty.get(property), false);
  79.                 return;
  80.             }
  81.  
  82.             if (ForgeRegistries.ITEMS.getValue(
  83.                     ResourceLocation.fromNamespaceAndPath("mmm", candle + "_candle_lit")) == item.asItem()) {
  84.                 state = state.setValue(property, candle);
  85.                 state = state.setValue(litToProperty.get(property), true);
  86.                 return;
  87.             }
  88.         }
  89.  
  90.         state = state.setValue(property, Candle.EMPTY);
  91.         state = state.setValue(litToProperty.get(property), false);
  92.     }
  93.  
  94.  
  95.     public static final EnumProperty<Candle> SHAMASH = EnumProperty.create("shamash", Candle.class);
  96.     public static final EnumProperty<Candle> CANDLE_2 = EnumProperty.create("candle_2", Candle.class);
  97.     public static final EnumProperty<Candle> CANDLE_3 = EnumProperty.create("candle_3", Candle.class);
  98.     public static final EnumProperty<Candle> CANDLE_4 = EnumProperty.create("candle_4", Candle.class);
  99.     public static final EnumProperty<Candle> CANDLE_5 = EnumProperty.create("candle_5", Candle.class);
  100.     public static final EnumProperty<Candle> CANDLE_6 = EnumProperty.create("candle_6", Candle.class);
  101.     public static final EnumProperty<Candle> CANDLE_7 = EnumProperty.create("candle_7", Candle.class);
  102.     public static final EnumProperty<Candle> CANDLE_8 = EnumProperty.create("candle_8", Candle.class);
  103.     public static final EnumProperty<Candle> CANDLE_9 = EnumProperty.create("candle_9", Candle.class);
  104.  
  105.     public static final BooleanProperty SHAMASH_LIT = BooleanProperty.create("shamash_lit");
  106.     public static final BooleanProperty CANDLE_2_LIT = BooleanProperty.create("candle_2_lit");
  107.     public static final BooleanProperty CANDLE_3_LIT = BooleanProperty.create("candle_3_lit");
  108.     public static final BooleanProperty CANDLE_4_LIT = BooleanProperty.create("candle_4_lit");
  109.     public static final BooleanProperty CANDLE_5_LIT = BooleanProperty.create("candle_5_lit");
  110.     public static final BooleanProperty CANDLE_6_LIT = BooleanProperty.create("candle_6_lit");
  111.     public static final BooleanProperty CANDLE_7_LIT = BooleanProperty.create("candle_7_lit");
  112.     public static final BooleanProperty CANDLE_8_LIT = BooleanProperty.create("candle_8_lit");
  113.     public static final BooleanProperty CANDLE_9_LIT = BooleanProperty.create("candle_9_lit");
  114.  
  115.     private final BiMap<EnumProperty<Candle>, BooleanProperty> litToProperty = HashBiMap.create(Map.of(
  116.             SHAMASH, SHAMASH_LIT,
  117.             CANDLE_2, CANDLE_2_LIT,
  118.             CANDLE_3, CANDLE_3_LIT,
  119.             CANDLE_4, CANDLE_4_LIT,
  120.             CANDLE_5, CANDLE_5_LIT,
  121.             CANDLE_6, CANDLE_6_LIT,
  122.             CANDLE_7, CANDLE_7_LIT,
  123.             CANDLE_8, CANDLE_8_LIT,
  124.             CANDLE_9, CANDLE_9_LIT
  125.     ));
  126.  
  127.     private final EnumProperty<Candle>[] CANDLES_A = new EnumProperty[]{
  128.             SHAMASH, CANDLE_2, CANDLE_3, CANDLE_4, CANDLE_5, CANDLE_6, CANDLE_7, CANDLE_8, CANDLE_9
  129.     };
  130.  
  131.     private final EnumProperty<Candle>[] A_CANDLES = new EnumProperty[]{
  132.             CANDLE_9, CANDLE_8, CANDLE_7, CANDLE_6, CANDLE_5, CANDLE_4, CANDLE_3, CANDLE_2, SHAMASH
  133.     };
  134.  
  135.     private final EnumProperty<Candle>[] B_CANDLES = new EnumProperty[]{
  136.             CANDLE_9, CANDLE_8, CANDLE_7, CANDLE_6, CANDLE_5, CANDLE_4, CANDLE_3, CANDLE_2
  137.     };
  138.  
  139.     public HanukiyaBlock(Properties properties) {
  140.         super(properties);
  141.         this.registerDefaultState(this.getStateDefinition().any().setValue(FACING, Direction.NORTH)
  142.                 .setValue(SHAMASH, Candle.EMPTY)
  143.                 .setValue(CANDLE_2, Candle.EMPTY).setValue(CANDLE_3, Candle.EMPTY)
  144.                 .setValue(CANDLE_4, Candle.EMPTY).setValue(CANDLE_5, Candle.EMPTY)
  145.                 .setValue(CANDLE_6, Candle.EMPTY).setValue(CANDLE_7, Candle.EMPTY)
  146.                 .setValue(CANDLE_8, Candle.EMPTY).setValue(CANDLE_9, Candle.EMPTY)
  147.                 .setValue(SHAMASH_LIT, false)
  148.                 .setValue(CANDLE_2_LIT, false).setValue(CANDLE_3_LIT, false)
  149.                 .setValue(CANDLE_4_LIT, false).setValue(CANDLE_5_LIT, false)
  150.                 .setValue(CANDLE_6_LIT, false).setValue(CANDLE_7_LIT, false)
  151.                 .setValue(CANDLE_8_LIT, false).setValue(CANDLE_9_LIT, false));
  152.     }
  153.  
  154.     @Override
  155.     protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) {
  156.         stateBuilder.add(FACING);
  157.         stateBuilder.add(SHAMASH);
  158.         stateBuilder.add(SHAMASH_LIT);
  159.         stateBuilder.add(CANDLE_2);
  160.         stateBuilder.add(CANDLE_2_LIT);
  161.         stateBuilder.add(CANDLE_3);
  162.         stateBuilder.add(CANDLE_3_LIT);
  163.         stateBuilder.add(CANDLE_4);
  164.         stateBuilder.add(CANDLE_4_LIT);
  165.         stateBuilder.add(CANDLE_5);
  166.         stateBuilder.add(CANDLE_5_LIT);
  167.         stateBuilder.add(CANDLE_6);
  168.         stateBuilder.add(CANDLE_6_LIT);
  169.         stateBuilder.add(CANDLE_7);
  170.         stateBuilder.add(CANDLE_7_LIT);
  171.         stateBuilder.add(CANDLE_8);
  172.         stateBuilder.add(CANDLE_8_LIT);
  173.         stateBuilder.add(CANDLE_9);
  174.         stateBuilder.add(CANDLE_9_LIT);
  175.     }
  176.  
  177.     @Override
  178.     public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) {
  179.         Direction direction = context.getHorizontalDirection();
  180.  
  181.         return this.defaultBlockState().setValue(FACING, direction);
  182.     }
  183.  
  184.     //-- Use method
  185.     @Override
  186.     public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult blockHitResult) {
  187.         ItemStack held = player.getItemInHand(hand);
  188.  
  189.         if (player.isShiftKeyDown()) {
  190.             if (held.isEmpty()) {
  191.                 if (state.getValue(SHAMASH) != Candle.EMPTY) {
  192.                     if (state.getValue(SHAMASH).isLit()) {
  193.                         player.getInventory().add(getCandle(state.getValue(SHAMASH)).asItem().getDefaultInstance());
  194.                     }
  195.                     state.setValue(SHAMASH, Candle.EMPTY);
  196.                 }
  197.             } else if (held.is(ModTags.Items.LIT_CANDLES) || held.is(ItemTags.CANDLES)) {
  198.                 takeCandle(state, player, SHAMASH, level, held);
  199.             }
  200.         } else if (held.is(ItemTags.SHOVELS)) {
  201.             EnumProperty<Candle> toChange = getNextLit(state);
  202.  
  203.             if (toChange != null) {
  204.                 state.setValue(litToProperty.get(toChange), false);
  205.                 if (level.isClientSide()) {
  206.                     held.hurtAndBreak(1, player, p -> p.broadcastBreakEvent(hand));
  207.                     if (!held.isDamageableItem()) {
  208.                         held.shrink(1);
  209.                     }
  210.                 }
  211.             }
  212.         } else if (held.is(Items.FLINT_AND_STEEL) || held.is(Items.FIRE_CHARGE)) {
  213.             EnumProperty<Candle> toChange = getNextUnLit(state);
  214.  
  215.             if (toChange != null) {
  216.                 state.setValue(litToProperty.get(toChange), true);
  217.                 if (!level.isClientSide()) {
  218.                     held.hurtAndBreak(1, player, p -> p.broadcastBreakEvent(hand));
  219.                     if (!held.isDamageableItem()) {
  220.                         held.shrink(1);
  221.                     }
  222.                 }
  223.             }
  224.         } else if (held.is(ItemTags.CANDLES) || held.is(ModTags.Items.LIT_CANDLES)) {
  225.             EnumProperty<Candle> property = getNextEmpty(state);
  226.             takeCandle(state, player, property, level, held);
  227.         } else if (held.isEmpty()) {
  228.             if (state.getBlock() instanceof HanukiyaBlock) {
  229.                 EnumProperty<Candle> prop = getNextFilled(state);
  230.                     if (!(prop == null)) {
  231.                         player.getInventory().add(state.getValue(prop).asItem().asItem().getDefaultInstance());
  232.                     state.setValue(prop, Candle.EMPTY);
  233.                 }
  234.             }
  235.         }
  236.  
  237.         player.setItemInHand(hand, held);
  238.  
  239.         level.setBlock(pos, state, 3);
  240.  
  241.         return InteractionResult.SUCCESS;
  242.     }
  243.  
  244.     @Override
  245.     public void playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
  246.         if (!level.isClientSide && !player.isCreative()) {
  247.             for (EnumProperty<Candle> prop : CANDLES_A) {
  248.                 Candle candle = state.getValue(prop);
  249.                 if (candle != Candle.EMPTY) {
  250.                     ItemLike item = candle.unlitItem();
  251.                     popResource(level, pos, item.asItem().getDefaultInstance());
  252.                 }
  253.             }
  254.         }
  255.         super.playerWillDestroy(level, pos, state, player);
  256.     }
  257.  
  258.     private EnumProperty<Candle> getNextEmpty(BlockState state) {
  259.         for (EnumProperty<Candle> property : CANDLES_A) {
  260.             if (state.getValue(property) == Candle.EMPTY) {
  261.                 return property;
  262.             }
  263.         }
  264.         return null;
  265.     }
  266.  
  267.     private EnumProperty<Candle> getNextFilled(BlockState state) {
  268.         for (EnumProperty<Candle> property : B_CANDLES) {
  269.             if (state.getValue(property) != Candle.EMPTY) {
  270.                 return property;
  271.             }
  272.         }
  273.         return null;
  274.     }
  275.  
  276.     private EnumProperty<Candle> getNextLit(BlockState state) {
  277.         for (EnumProperty<Candle> property : A_CANDLES) {
  278.             if (state.getValue(property).isLit()) {
  279.                 return property;
  280.             }
  281.         }
  282.         return null;
  283.     }
  284.  
  285.     private EnumProperty<Candle> getNextUnLit(BlockState state) {
  286.         for (EnumProperty<Candle> property : CANDLES_A) {
  287.             if (!state.getValue(property).isLit()) {
  288.                 return property;
  289.             }
  290.         }
  291.         return null;
  292.     }
  293.  
  294.     public void takeCandle(BlockState state, Player player, EnumProperty<Candle> property, Level level, ItemStack toChange) {
  295.         Item compareTo = toChange.getItem();
  296.         if (!level.isClientSide()) {
  297.             toChange.shrink(1);
  298.         }
  299.         convertToState(state, compareTo, property);
  300.     }
  301.  
  302.     public ItemLike getCandle(Candle type) {
  303.         return type.asItem();
  304.     }
  305. }
Add Comment
Please, Sign In to add comment