Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.MrGise.mmm.block;
- import com.google.common.collect.BiMap;
- import com.google.common.collect.HashBiMap;
- import net.MrGise.mmm.registry.back.ModTags;
- import net.minecraft.core.BlockPos;
- import net.minecraft.core.Direction;
- import net.minecraft.resources.ResourceLocation;
- import net.minecraft.tags.ItemTags;
- import net.minecraft.util.StringRepresentable;
- import net.minecraft.world.InteractionHand;
- import net.minecraft.world.InteractionResult;
- import net.minecraft.world.entity.player.Player;
- import net.minecraft.world.item.Item;
- import net.minecraft.world.item.ItemStack;
- import net.minecraft.world.item.Items;
- import net.minecraft.world.item.context.BlockPlaceContext;
- import net.minecraft.world.level.ItemLike;
- import net.minecraft.world.level.Level;
- import net.minecraft.world.level.block.Block;
- import net.minecraft.world.level.block.HorizontalDirectionalBlock;
- import net.minecraft.world.level.block.state.BlockState;
- import net.minecraft.world.level.block.state.StateDefinition;
- import net.minecraft.world.level.block.state.properties.BooleanProperty;
- import net.minecraft.world.level.block.state.properties.EnumProperty;
- import net.minecraft.world.phys.BlockHitResult;
- import net.minecraftforge.registries.ForgeRegistries;
- import org.jetbrains.annotations.Nullable;
- import java.util.HashMap;
- import java.util.Map;
- public class HanukiyaBlock extends HorizontalDirectionalBlock {
- public enum Candle implements StringRepresentable {
- NONE,
- WHITE, LIGHT_GRAY,
- GRAY, BLACK,
- BROWN, RED,
- ORANGE, YELLOW,
- LIME, GREEN,
- CYAN, LIGHT_BLUE,
- BLUE, PURPLE,
- MAGENTA, PINK,
- EMPTY;
- public Item asItem() {
- if (this == EMPTY) return Items.AIR;
- return ForgeRegistries.ITEMS.getValue(
- this.isLit()
- ? ResourceLocation.fromNamespaceAndPath("mmm", this.getSerializedName() + "_candle_lit")
- : ResourceLocation.withDefaultNamespace(this.getSerializedName() + "_candle")
- );
- }
- public Item unlitItem() {
- if (this == EMPTY) return Items.AIR;
- return ForgeRegistries.ITEMS.getValue(
- ResourceLocation.withDefaultNamespace(this.getSerializedName() + "_candle")
- );
- }
- public boolean isLit() {
- return this.name().endsWith("_LIT");
- }
- @Override
- public String getSerializedName() {
- return name().toLowerCase();
- }
- }
- public void convertToState(BlockState state, ItemLike item, EnumProperty<Candle> property) {
- for (Candle candle : Candle.values()) {
- if (candle == Candle.EMPTY) continue;
- if (candle.unlitItem() == item.asItem()) {
- state = state.setValue(property, candle);
- state = state.setValue(litToProperty.get(property), false);
- return;
- }
- if (ForgeRegistries.ITEMS.getValue(
- ResourceLocation.fromNamespaceAndPath("mmm", candle + "_candle_lit")) == item.asItem()) {
- state = state.setValue(property, candle);
- state = state.setValue(litToProperty.get(property), true);
- return;
- }
- }
- state = state.setValue(property, Candle.EMPTY);
- state = state.setValue(litToProperty.get(property), false);
- }
- public static final EnumProperty<Candle> SHAMASH = EnumProperty.create("shamash", Candle.class);
- public static final EnumProperty<Candle> CANDLE_2 = EnumProperty.create("candle_2", Candle.class);
- public static final EnumProperty<Candle> CANDLE_3 = EnumProperty.create("candle_3", Candle.class);
- public static final EnumProperty<Candle> CANDLE_4 = EnumProperty.create("candle_4", Candle.class);
- public static final EnumProperty<Candle> CANDLE_5 = EnumProperty.create("candle_5", Candle.class);
- public static final EnumProperty<Candle> CANDLE_6 = EnumProperty.create("candle_6", Candle.class);
- public static final EnumProperty<Candle> CANDLE_7 = EnumProperty.create("candle_7", Candle.class);
- public static final EnumProperty<Candle> CANDLE_8 = EnumProperty.create("candle_8", Candle.class);
- public static final EnumProperty<Candle> CANDLE_9 = EnumProperty.create("candle_9", Candle.class);
- public static final BooleanProperty SHAMASH_LIT = BooleanProperty.create("shamash_lit");
- public static final BooleanProperty CANDLE_2_LIT = BooleanProperty.create("candle_2_lit");
- public static final BooleanProperty CANDLE_3_LIT = BooleanProperty.create("candle_3_lit");
- public static final BooleanProperty CANDLE_4_LIT = BooleanProperty.create("candle_4_lit");
- public static final BooleanProperty CANDLE_5_LIT = BooleanProperty.create("candle_5_lit");
- public static final BooleanProperty CANDLE_6_LIT = BooleanProperty.create("candle_6_lit");
- public static final BooleanProperty CANDLE_7_LIT = BooleanProperty.create("candle_7_lit");
- public static final BooleanProperty CANDLE_8_LIT = BooleanProperty.create("candle_8_lit");
- public static final BooleanProperty CANDLE_9_LIT = BooleanProperty.create("candle_9_lit");
- private final BiMap<EnumProperty<Candle>, BooleanProperty> litToProperty = HashBiMap.create(Map.of(
- SHAMASH, SHAMASH_LIT,
- CANDLE_2, CANDLE_2_LIT,
- CANDLE_3, CANDLE_3_LIT,
- CANDLE_4, CANDLE_4_LIT,
- CANDLE_5, CANDLE_5_LIT,
- CANDLE_6, CANDLE_6_LIT,
- CANDLE_7, CANDLE_7_LIT,
- CANDLE_8, CANDLE_8_LIT,
- CANDLE_9, CANDLE_9_LIT
- ));
- private final EnumProperty<Candle>[] CANDLES_A = new EnumProperty[]{
- SHAMASH, CANDLE_2, CANDLE_3, CANDLE_4, CANDLE_5, CANDLE_6, CANDLE_7, CANDLE_8, CANDLE_9
- };
- private final EnumProperty<Candle>[] A_CANDLES = new EnumProperty[]{
- CANDLE_9, CANDLE_8, CANDLE_7, CANDLE_6, CANDLE_5, CANDLE_4, CANDLE_3, CANDLE_2, SHAMASH
- };
- private final EnumProperty<Candle>[] B_CANDLES = new EnumProperty[]{
- CANDLE_9, CANDLE_8, CANDLE_7, CANDLE_6, CANDLE_5, CANDLE_4, CANDLE_3, CANDLE_2
- };
- public HanukiyaBlock(Properties properties) {
- super(properties);
- this.registerDefaultState(this.getStateDefinition().any().setValue(FACING, Direction.NORTH)
- .setValue(SHAMASH, Candle.EMPTY)
- .setValue(CANDLE_2, Candle.EMPTY).setValue(CANDLE_3, Candle.EMPTY)
- .setValue(CANDLE_4, Candle.EMPTY).setValue(CANDLE_5, Candle.EMPTY)
- .setValue(CANDLE_6, Candle.EMPTY).setValue(CANDLE_7, Candle.EMPTY)
- .setValue(CANDLE_8, Candle.EMPTY).setValue(CANDLE_9, Candle.EMPTY)
- .setValue(SHAMASH_LIT, false)
- .setValue(CANDLE_2_LIT, false).setValue(CANDLE_3_LIT, false)
- .setValue(CANDLE_4_LIT, false).setValue(CANDLE_5_LIT, false)
- .setValue(CANDLE_6_LIT, false).setValue(CANDLE_7_LIT, false)
- .setValue(CANDLE_8_LIT, false).setValue(CANDLE_9_LIT, false));
- }
- @Override
- protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> stateBuilder) {
- stateBuilder.add(FACING);
- stateBuilder.add(SHAMASH);
- stateBuilder.add(SHAMASH_LIT);
- stateBuilder.add(CANDLE_2);
- stateBuilder.add(CANDLE_2_LIT);
- stateBuilder.add(CANDLE_3);
- stateBuilder.add(CANDLE_3_LIT);
- stateBuilder.add(CANDLE_4);
- stateBuilder.add(CANDLE_4_LIT);
- stateBuilder.add(CANDLE_5);
- stateBuilder.add(CANDLE_5_LIT);
- stateBuilder.add(CANDLE_6);
- stateBuilder.add(CANDLE_6_LIT);
- stateBuilder.add(CANDLE_7);
- stateBuilder.add(CANDLE_7_LIT);
- stateBuilder.add(CANDLE_8);
- stateBuilder.add(CANDLE_8_LIT);
- stateBuilder.add(CANDLE_9);
- stateBuilder.add(CANDLE_9_LIT);
- }
- @Override
- public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) {
- Direction direction = context.getHorizontalDirection();
- return this.defaultBlockState().setValue(FACING, direction);
- }
- //-- Use method
- @Override
- public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult blockHitResult) {
- ItemStack held = player.getItemInHand(hand);
- if (player.isShiftKeyDown()) {
- if (held.isEmpty()) {
- if (state.getValue(SHAMASH) != Candle.EMPTY) {
- if (state.getValue(SHAMASH).isLit()) {
- player.getInventory().add(getCandle(state.getValue(SHAMASH)).asItem().getDefaultInstance());
- }
- state.setValue(SHAMASH, Candle.EMPTY);
- }
- } else if (held.is(ModTags.Items.LIT_CANDLES) || held.is(ItemTags.CANDLES)) {
- takeCandle(state, player, SHAMASH, level, held);
- }
- } else if (held.is(ItemTags.SHOVELS)) {
- EnumProperty<Candle> toChange = getNextLit(state);
- if (toChange != null) {
- state.setValue(litToProperty.get(toChange), false);
- if (level.isClientSide()) {
- held.hurtAndBreak(1, player, p -> p.broadcastBreakEvent(hand));
- if (!held.isDamageableItem()) {
- held.shrink(1);
- }
- }
- }
- } else if (held.is(Items.FLINT_AND_STEEL) || held.is(Items.FIRE_CHARGE)) {
- EnumProperty<Candle> toChange = getNextUnLit(state);
- if (toChange != null) {
- state.setValue(litToProperty.get(toChange), true);
- if (!level.isClientSide()) {
- held.hurtAndBreak(1, player, p -> p.broadcastBreakEvent(hand));
- if (!held.isDamageableItem()) {
- held.shrink(1);
- }
- }
- }
- } else if (held.is(ItemTags.CANDLES) || held.is(ModTags.Items.LIT_CANDLES)) {
- EnumProperty<Candle> property = getNextEmpty(state);
- takeCandle(state, player, property, level, held);
- } else if (held.isEmpty()) {
- if (state.getBlock() instanceof HanukiyaBlock) {
- EnumProperty<Candle> prop = getNextFilled(state);
- if (!(prop == null)) {
- player.getInventory().add(state.getValue(prop).asItem().asItem().getDefaultInstance());
- state.setValue(prop, Candle.EMPTY);
- }
- }
- }
- player.setItemInHand(hand, held);
- level.setBlock(pos, state, 3);
- return InteractionResult.SUCCESS;
- }
- @Override
- public void playerWillDestroy(Level level, BlockPos pos, BlockState state, Player player) {
- if (!level.isClientSide && !player.isCreative()) {
- for (EnumProperty<Candle> prop : CANDLES_A) {
- Candle candle = state.getValue(prop);
- if (candle != Candle.EMPTY) {
- ItemLike item = candle.unlitItem();
- popResource(level, pos, item.asItem().getDefaultInstance());
- }
- }
- }
- super.playerWillDestroy(level, pos, state, player);
- }
- private EnumProperty<Candle> getNextEmpty(BlockState state) {
- for (EnumProperty<Candle> property : CANDLES_A) {
- if (state.getValue(property) == Candle.EMPTY) {
- return property;
- }
- }
- return null;
- }
- private EnumProperty<Candle> getNextFilled(BlockState state) {
- for (EnumProperty<Candle> property : B_CANDLES) {
- if (state.getValue(property) != Candle.EMPTY) {
- return property;
- }
- }
- return null;
- }
- private EnumProperty<Candle> getNextLit(BlockState state) {
- for (EnumProperty<Candle> property : A_CANDLES) {
- if (state.getValue(property).isLit()) {
- return property;
- }
- }
- return null;
- }
- private EnumProperty<Candle> getNextUnLit(BlockState state) {
- for (EnumProperty<Candle> property : CANDLES_A) {
- if (!state.getValue(property).isLit()) {
- return property;
- }
- }
- return null;
- }
- public void takeCandle(BlockState state, Player player, EnumProperty<Candle> property, Level level, ItemStack toChange) {
- Item compareTo = toChange.getItem();
- if (!level.isClientSide()) {
- toChange.shrink(1);
- }
- convertToState(state, compareTo, property);
- }
- public ItemLike getCandle(Candle type) {
- return type.asItem();
- }
- }
Add Comment
Please, Sign In to add comment