package com.thebrokenrail.twine.util; import com.google.common.collect.Lists; import com.thebrokenrail.twine.Twine; import net.minecraft.block.Block; import net.minecraft.block.Blocks; import net.minecraft.block.EnderChestBlock; import net.minecraft.block.ShulkerBoxBlock; import net.minecraft.block.entity.ChestBlockEntity; import net.minecraft.block.entity.ShulkerBoxBlockEntity; import net.minecraft.entity.Entity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; import net.minecraft.screen.GenericContainerScreenHandler; import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ShulkerBoxScreenHandler; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.SoundEvent; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.DyeColor; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; public class BoatChestMode { public interface BoatScreenHandlerFactory { ScreenHandler createMenu(int syncId, PlayerInventory inv, PlayerEntity player, BoatUtil boat); } private static final ArrayList values = new ArrayList<>(); public static final BoatChestMode NONE = new BoatChestMode(Blocks.AIR, null, null, null); static { BoatScreenHandlerFactory chestScreenHandler = (i, playerInventory, playerEntity, boat) -> GenericContainerScreenHandler.createGeneric9x3(i, playerInventory, boat.getChestInventory()); ChestBlockEntity chest = new ChestBlockEntity(); Function chestScreenHandlerName = stack -> stack.hasCustomName() ? stack.getName() : chest.getName(); new BoatChestMode(Blocks.ENDER_CHEST, (i, playerInventory, playerEntity, boat) -> GenericContainerScreenHandler.createGeneric9x3(i, playerInventory, new EnderChestInventoryWrapper((Entity) boat, playerEntity.getEnderChestInventory())), stack -> EnderChestBlock.CONTAINER_NAME, player -> Twine.ENDER_CHEST_BOAT_CRITERION.trigger(player)); new BoatChestMode(Blocks.CHEST, true, false, 27, chestScreenHandler, chestScreenHandlerName, player -> Twine.CHEST_BOAT_CRITERION.trigger(player), SoundEvents.BLOCK_CHEST_OPEN, SoundEvents.BLOCK_CHEST_CLOSE); new BoatChestMode(Blocks.TRAPPED_CHEST, true, false, 27, chestScreenHandler, chestScreenHandlerName, player -> Twine.CHEST_BOAT_CRITERION.trigger(player), SoundEvents.BLOCK_CHEST_OPEN, SoundEvents.BLOCK_CHEST_CLOSE); ShulkerBoxBlockEntity shulker = new ShulkerBoxBlockEntity(); List colors = Lists.asList(null, DyeColor.values()); for (DyeColor value : colors) { new BoatChestMode(ShulkerBoxBlock.get(value), true, true, 27, (i, playerInventory, playerEntity, boat) -> new ShulkerBoxScreenHandler(i, playerInventory, boat.getChestInventory()), stack -> stack.hasCustomName() ? stack.getName() : shulker.getName(), player -> Twine.SHULKER_BOX_BOAT_CRITERION.trigger(player), SoundEvents.BLOCK_SHULKER_BOX_OPEN, SoundEvents.BLOCK_SHULKER_BOX_CLOSE); } } private final Block block; private final boolean hasItems; private final boolean containsItems; private final int size; private final BoatScreenHandlerFactory screenHandlerFactory; private final Function screenHandlerNameFactory; private final Consumer advancementTrigger; private final int id; private final SoundEvent openSound; private final SoundEvent closeSound; private BoatChestMode(Block block, boolean hasItems, boolean containsItems, int size, BoatScreenHandlerFactory screenHandlerFactory, Function screenHandlerNameFactory, Consumer advancementTrigger, SoundEvent openSound, SoundEvent closeSound) { this.screenHandlerFactory = screenHandlerFactory; this.size = size; this.screenHandlerNameFactory = screenHandlerNameFactory; this.advancementTrigger = advancementTrigger; this.openSound = openSound; this.closeSound = closeSound; this.id = values.size(); this.block = block; this.hasItems = hasItems; this.containsItems = containsItems; values.add(this); } private BoatChestMode(Block block, BoatScreenHandlerFactory screenHandlerFactory, Function screenHandlerNameFactory, Consumer advancementTrigger) { this(block,false, false, 0, screenHandlerFactory, screenHandlerNameFactory, advancementTrigger, null, null); } public int getSize() { return size; } public BoatScreenHandlerFactory getScreenHandlerFactory() { return screenHandlerFactory; } public Text getScreenHandlerName(ItemStack stack) { return screenHandlerNameFactory.apply(stack); } public void triggerAdvancement(ServerPlayerEntity player) { if (advancementTrigger != null) { advancementTrigger.accept(player); } } public SoundEvent getOpenSound() { return openSound; } public SoundEvent getCloseSound() { return closeSound; } public Block getBlock() { return block; } public boolean hasItems() { return hasItems; } public boolean containsItems() { return containsItems; } public int getID() { return id; } public static BoatChestMode valueOf(Block block) { for (BoatChestMode mode : values) { if (mode.getBlock() == block) { return mode; } } return NONE; } public static BoatChestMode valueOf(int index) { BoatChestMode mode = values.get(index); return mode != null ? mode : NONE; } }