Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public final class HibachiBlock extends Block {
- public HibachiBlock (AbstractBlock.Settings settings) {
- super (settings);
- }
- protected void neighborUpdate (BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
- super.neighborUpdate(state, world, pos, sourceBlock, sourcePos, notify);
- this.tryLight(world, pos);
- }
- protected void onBlockAdded (BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
- super.onBlockAdded(state, world, pos, oldState, notify);
- this.tryLight(world, pos);
- }
- protected void randomTick (BlockState state, ServerWorld world, BlockPos pos, Random random) {
- super.randomTick(state, world, pos, random);
- this.tryLight(world, pos);
- }
- private void tryLight (World world, BlockPos pos) {
- if (world.isReceivingRedstonePower(pos) && world.getBlockState(pos.up()).isAir())
- world.setBlockState(Blocks.FIRE.getDefaultState());
- }
- }
- public final class BlockDispenserBlock extends HorizontalFacingBlock implements BlockEntityProvider {
- public BlockDispenserBlock (AbstractBlock.Settings settings) {
- super (settings);
- }
- protected void neighborUpdate (BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
- super.neighborUpdate(state, world, pos, sourceBlock, sourcePos, notify);
- this.tryDispense(world, pos);
- }
- protected void onBlockAdded (BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
- super.onBlockAdded(state, world, pos, oldState, notify);
- this.tryDispense(world, pos);
- }
- protected void randomTick (BlockState state, ServerWorld world, BlockPos pos, Random random) {
- super.randomTick(state, world, pos, random);
- this.tryDispense(world, pos);
- }
- private void tryDispense (World world, BlockPos pos) {
- if (!world.getBlockState(pos).hasProperty(HorizontalFacingBlock.FACING)) return;
- if (world.getRecievedStrongRedstonePower(pos) > 0) {
- List<Entity> entities = world.getNonSpectatingEntities(Entity.class, new Box(pos.offset(1L, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING))));
- if (entities.size() > 0) {
- boolean entityConsumed = false;
- for (Entity e : entities) {
- if (!entityConsumed)
- this.getOrCreateBlockEntity(world, pos).addItem(BlockDispenserBehaviors.ENTITY_COLLECTION.get(e.getClass()).collect(world, pos, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING), e));
- if (BlockDispenserEntityBehaviors.get(e.getClass()).consumesEntity()) entityConsumed = true;
- }
- if (entityConsumed) return;
- }
- if (world.getBlockState(pos.offset(1L, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING))).isIn(BlockTags.REPLACEABLE)) {
- Item next = this.getOrCreateBlockEntity(world, pos).next().getItem();
- if (BlockDispenserBehaviors.DISPENSING.get(next) != null) {
- BlockDispenserBehaviors.DISPENSING.get(next).dispense(world, pos, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING));
- } else if (BlockDispenserBehaviors.DISPENSING_BY_CLASS.get(next) != null) {
- BlockDispenserBehaviors.DISPENSING_BY_CLASS.get(next.getClass()).dispense(world, pos, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING));
- } else {
- if (next instanceof BlockItem)
- world.setBlockState(Block.getBlockForItem(next).getDefaultState());
- else
- Block.dropStack(world, pos.offset(1L, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING)), world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING), new ItemStack(next));
- }
- } else {
- Block next = world.getBlockState(pos.offset(1L, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING))).getBlock();
- if (BlockDispenserBehaviors.COLLECTION.get(next) != null)
- this.getOrCreateBlockEntity(world, pos).addItem(BlockDispenserBehaviors.COLLECTION.get(next).collect(world, pos, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING)));
- else if (BlockDispenserBehaviors.COLLECTION_BY_CLASS.get(next) != null)
- this.getOrCreateBlockEntity(world, pos).addItem(BlockDispenserBehaviors.COLLECTION_BY_CLASS.get(next.getClass()).collect(world, pos, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING)));
- else
- world.destroyBlock(pos.offset(1L, world.getBlockState(pos).getValue(HorizontalFacingBlock.FACING)), false);
- }
- }
- }
- public BlockEntity newBlockEntity (BlockPos pos, BlockState state) {
- return new BlockEntity(pos, state);
- }
- public BlockEntity getOrCreateBlockEntity (World world, BlockPos pos) {
- if (world.getBlockEntity(pos) instanceof BlockEntity be)
- return be;
- else
- return this.newBlockEntity(pos, world.getBlockState(pos));
- }
- @FunctionalInterface
- public static interface EntityBehavior {
- ItemStack collect (World world, BlockPos pos, Direction d, Entity e);
- default boolean consumesEntity () {
- return true;
- }
- }
- @FunctionalInterface
- public static interface DispenseBehavior {
- void dispense (World world, BlockPos pos, Direction d);
- }
- @FunctionalInterface
- public static interface CollectBehavior {
- ItemStack collect (World world, BlockPos pos, Direction d);
- }
- public static class BlockEntity extends net.minecraft.block.entity.BlockEntity implements Iterator<Item> {
- private DefaultedList<ItemStack> items = DefaultedList.<ItemStack>ofSize(9, ItemStack.EMPTY);
- private int index = 0;
- public BlockEntity (BlockPos pos, BlockState state) {
- super (BetterThanWolvesBlockEntities.BLOCK_DISPENSER, pos, state);
- }
- public boolean hasNext () {
- return true;
- }
- public Item next () {
- if (this.index == 8) this.index = -1;
- Item i = this.items.get(++this.index);
- this.items.set(this.index, this.items.get(this.index).shrink(1));
- return i;
- }
- public void addItem (ItemStack item) {
- ItemStack copy = item.copy();
- for (ItemStack i : this.items) {
- if (i.getItem() == copy.getItem() || i.isEmpty()) {
- if ((i.getCount() + copy.getCount()) <= copy.getMaxCount()) {
- this.items.set(this.items.indexOf(i), copy.grow(i.getCount()));
- } else {
- this.items.set(this.items.indexOf(i), copy.setCount(i.getMaxCount()));
- copy.setCount(copy.getCount() - copy.getMaxCount());
- }
- }
- }
- if (copy.getCount() > 0)
- Block.dropStack(this.getWorld(), this.pos, this.state.hasProperty(HorizontalFacingBlock.FACING) ? this.state.getValue(HorizontalFacingBlock.FACING) : Direction.UP, copy);
- }
- }
- }
- public class BlockDispenserBehaviors {
- public static Map<Class<? extends Entity>, BlockDispenserBlock.EntityBehavior> ENTITY_COLLECTION = Map.<Class<? extends Entity>, BlockDispenserBlock.EntityBehavior>of();
- public static Map<Item, BlockDispenserBlock.DispenseBehavior> DISPENSING = Map.<Item, BlockDispenserBlock.DispenseBehavior>of();
- public static Map<Class<? extends Item>, BlockDispenserBlock.DispenseBehavior> DISPENSING_BY_CLASS = Map.<Class<? extends Item>, BlockDispenserBlock.DispenseBehavior>of();
- public static Map<Block, BlockDispenserBlock.CollectBehavior> COLLECTION = Map.<Block, BlockDispenserBlock.CollectBehavior>of();
- static {
- ENTITY_COLLECTION.put(WolfEntity.class, (w, p, d, e) -> {
- e.discard();
- Block.dropStack(w, p.offset(1L, d), new ItemStack(Items.STRING, 2));
- return new ItemStack(BetterThanWolvesItems.COMPANION_CUBE);
- });
- ENTITY_COLLECTION.put(ChickenEntity.class, (w, p, d, e) -> {
- e.discard();
- return new ItemStack(Items.EGG);
- });
- ENTITY_COLLECTION.put(SheepEntity.class, (w, p, d, e) -> {
- DyeColor color = ((SheepEntity)e).getColor();
- e.discard();
- Block.dropStack(w, p.offset(1L, d), new ItemStack(Items.STRING));
- return ((SheepEntity)e).shearable() ? new ItemStack(BlockDispenserBlock.getWool(color)) : ItemStack.EMPTY;
- });
- DISPENSING_BY_CLASS.put(WindmillItem.class, (w, p, d, i) -> {
- if (((WindmillItem)i).canPlace(w, p.offset(1L, d)))
- ((WindmillItem)i).tryPlace(p.offset(1L, d));
- else
- Block.dropStack(w, p, d, new ItemStack(BetterThanWolvesItems.WINDMILL));
- });
- DISPENSING_BY_CLASS.put(VerticalWindmillItem.class, (w, p, d, i) -> { Block.dropStack(w, p, d, new ItemStack(i)); });
- DISPENSING_BY_CLASS.put(WaterWheelItem.class, (w, p, d, i) -> {
- if (((WaterWheelItem)i).canPlace(w, p.offset(1L, d)))
- ((WaterWheelItem)i).tryPlace(p.offset(1L, d));
- else
- Block.dropStack(w, p, d, new ItemStack(BetterThanWolvesItems.WATER_WHEEL));
- });
- DISPENSING_BY_CLASS.put(BedItem.class, (w, p, d, i) -> {
- if (w.getBlockState(p.offset(1L, d)).isIn(BlockTags.REPLACEABLE) && w.getBlockState(p.offset(2L, d)).isIn(BlockTags.REPLACEABLE) && w.getBlockState(p.offset(1L, d).down()).isSolid() && w.getBlockState(p.offset(2L, d).down()).isSolid()) {
- w.setBlockState(p.offset(1L, d), Block.getBlockForItem(i).getDefaultState().with(HorizontalFacingBlock.FACING, d).with(BedBlock.PART, BedPart.FOOT).with(BedBlock.OCCUPIED, Boolean.FALSE), 11);
- w.setBlockState(p.offset(2L, d), Block.getBlockForItem(i).getDefaultState().with(HorizontalFacingBlock.FACING, d).with(BedBlock.PART, BedPart.HEAD).with(BedBlock.OCCUPIED, Boolean.FALSE), 11);
- } else {
- Block.dropStack(w, p, d, new ItemStack(i));
- }
- });
- DISPENSING_BY_CLASS.put(HangingSignItem.class, (w, p, d, i) -> { Block.dropStack(w, p, d, new ItemStack(i)); });
- DISPENSING_BY_CLASS.put(SignItem.class, (w, p, d, i) -> {
- Boolean waterlogged;
- if (w.getFluidState(p.offset(1L, d)).getType() == Fluids.WATER)
- waterlogged = Boolean.TRUE;
- else
- waterlogged = Boolean.FALSE;
- w.setBlockState(p.offset(1L, d), Block.getBlockForItem(i).getDefaultState().with(AbstractSignBlock.WATERLOGGED, waterlogged).with(WallSignBlock.FACING, d)), 11);
- });
- DISPENSING_BY_CLASS.put(TallBlockItem.class, (w, p, d, i) -> { Block.dropStack(w, p, d, new ItemStack(i)); });
- DISPENSING_BY_CLASS.put(DyeItem.class, (w, p, d, i) -> {
- BlockState b = w.getBlockState(p.offset(1L, d));
- if (b.isIn(BlockTags.WOOL)) {
- w.setBlockState(p.offset(1L, d), BlockDispenserBlock.getWool(((DyeItem)i).getColor()), 11);
- } else if (b instanceof AbstractSignBlock) {
- SignBlockEntity sign = w.getBlockEntity(p.offset(1L, d));
- sign.setText(sign.getText(true).setColor(((DyeItem)i).getColor()), true);
- sign.setText(sign.getText(false).setColor(((DyeItem)i).getColor()), false);
- } else {
- Block.dropStack(w, p, d, new ItemStack(i));
- }
- });
- }
- }
- public class CauldronBlock extends BaseEntityBlock {
- public NamedScreenHandlerFactory createScreenHandlerFactory (BlockState state, World world, BlockPos pos) {
- return this.getOrCreateBlockEntity(world, pos);
- }
- public static class BlockEntity extends net.minecraft.block.entity.BlockEntity implements NamedScreenHandlerFactory {
- private Inventory inventory = this.new Inventory();
- DefaultedList<ItemStack> items = DefaultedList.<ItemStack>ofSize(27, ItemStack.EMPTY);
- private boolean active = false;
- private int[] activeSlots;
- private int[] activeSlotDecreases;
- private int progress = 0;
- private int targetProgress;
- private ItemStack output;
- public BlockEntity (BlockPos pos, BlockState state) {
- super (BetterThanWolvesBlockEntities.CAULDRON, pos, state);
- this.notifyItemsChanged(-2);
- }
- public Text getDisplayName () {
- return Text.translatable("btwce:block.entity.cauldron.name");
- }
- public ScreenHandler createMenu (int syncId, PlayerInventory inventory, Player player) {
- return new GenericContainerScreen(new GenericContainerScreenHandler(MenuType<GenericContainerScreenHandler>, syncId, inventory, this.inventory, 3), inventory, this.getDisplayName());
- }
- public void tick () {
- if (this.active)
- this.activeTick();
- else
- this.notifyItemsChanged(-2);
- }
- private void activeTick () {
- this.progress++;
- if (this.progress >= targetProgress) {
- this.active = false;
- this.progress = 0;
- this.activeRecipe.assemble(this.inventory);
- }
- }
- void notifyItemsChanged (int slotIsh) {
- switch (slotIsh) {
- case -2:
- this.active = this.resetActiveRecipes();
- this.getWorld().updateNeighborsAlways(this.pos, BetterThanWolvesBlocks.CAULDRON);
- break;
- case -1:
- this.getWorld().updateNeighborsAlways(this.pos, BetterThanWolvesBlocks.CAULDRON);
- break;
- default:
- if (Arrays.binarySearch(this.activeItems, slotIsh) & 0x80000000 != 0)
- this.notifyItemsChanged(-2);
- else
- this.getWorld().updateNeighborsAlways(this.pos, BetterThanWolvesBlocks.CAULDRON);
- }
- }
- private boolean resetActiveRecipes () {
- List<RecipeHolder<CauldronRecipe>> recipes = new RecipeManager(Registries.REGISTRY.getReadOnlyWrapper()).listAllOfType(CauldronRecipe.TYPE);
- for (RecipeHolder<CauldronRecipe> r : recipes) {
- if (r.value().test(this.inventory)) {
- this.activeRecipe = r;
- this.targetProgress = r.getTickTime();
- this.progress = 0;
- return true;
- }
- }
- return false;
- }
- public int firstAvailableSlot (ItemStack item) {
- return Math.min(this.items.indexOf(item), this.items.indexOf(ItemStack.EMPTY));
- }
- private class Inventory extends net.minecraft.inventory.Inventory {
- public Inventory () {}
- public int size () {
- return this.super.items.size();
- }
- public boolean isEmpty () {
- return this.super.items.isEmpty();
- }
- public ItemStack getStack (int slot) {
- return this.super.items.get(slot);
- }
- public ItemStack removeStack (int slot, int amount) {
- this.super.notifyItemsChanged(slot);
- this.super.items.get(slot).shrink(amount);
- return this.super.items.get(slot);
- }
- public ItemStack removeStack (int slot) {
- this.super.notifyItemsChanged(slot);
- return this.super.items.remove(slot);
- }
- public void setStack (int slot, ItemStack item) {
- this.super.notifyItemsChanged(slot);
- this.super.items.set(slot, item);
- }
- public void markDirty () {
- this.super.notifyItemsChanged(-1);
- }
- public boolean canPlayerUse (PlayerEntity player) {
- return net.minecraft.inventory.Inventory.canPlayerUse(this.super, player, 5.0F);
- }
- public Stream<ItemStack> stream () {
- return this.super.items.stream();
- }
- public CauldronBlock.BlockEntity getOwner () {
- return this.super;
- }
- public DetachedInventory detachedCopy () {
- return new DetachedInventory(List.<ItemStack>copyOf(this.super.items));
- }
- public static class DetachedInventory extends CauldronBlock.BlockEntity.Inventory {
- public DetachedInventory (ItemStack items) {
- this.list = items;
- }
- public int size () {
- return this.list.size();
- }
- public boolean isEmpty () {
- return this.list.isEmpty();
- }
- public ItemStack getStack (int slot) {
- return this.list.get(slot);
- }
- public ItemStack removeStack (int slot, int amount) {
- this.list.get(slot).shrink(amount);
- return this.list.get(slot);
- }
- public ItemStack removeStack (int slot) {
- return this.list.remove(slot);
- }
- public void setStack (int slot, ItemStack item) {
- this.list.set(slot, item);
- }
- public void markDirty () {}
- public boolean canPlayerUse (PlayerEntity player) {
- return true;
- }
- public Stream<ItemStack> stream () {
- return this.list.stream();
- }
- public CauldronBlock.BlockEntity getOwner () {
- return null;
- }
- public DetachedInventory detachedCopy () {
- return new DetachedInventory(List.<ItemStack>copyOf(this.list));
- }
- }
- }
- }
- }
- public class CauldronRecipe implements Recipe<DefaultedList<ItemStack>> {
- public static final RecipeType<CauldronRecipe> TYPE = RecipeType.register("btwce:cauldron_cooking");
- private DefaultedList<ItemStack> items;
- private ItemStack result;
- private DefaultedList<ItemStack> remainders;
- private int time;
- public CauldronRecipe (DefaultedList<ItemStack> inputs, ItemStack output, DefaultedList<ItemStack> remainders, int time) {
- this.items = inputs;
- this.result = output;
- this.remainders = remainders;
- this.time = time;
- }
- @Override
- public boolean matches (DefaultedList<ItemStack> inputs, World world) {
- return this.items.equals(inputs);
- }
- @Override
- public ItemStack assemble (DefaultedList<ItemStack> inputs, RegistryWrapper.WrapperLookup registry) {
- return this.result;
- }
- @Override
- public boolean fits (int width, int height) {
- return width >= 9 && height >= 3;
- }
- @Override
- public ItemStack getResultItem (RegistryWrapper.WrapperLookup registry) {
- return this.result;
- }
- @Override
- public ItemStack getRemainder (DefaultedList<ItemStack> inputs) {
- return this.result;
- }
- @Override
- public Serializer getSerializer () {
- return new Serializer();
- }
- @Override
- public RecipeType<CauldronRecipe> getType () {
- return TYPE;
- }
- public boolean test (CauldronBlock.BlockEntity.Inventory inventory) {
- try {
- return this.assemble(inventory.detachedCopy());
- } catch (NullPointerException npe) {
- return true;
- }
- }
- public boolean assemble (CauldronBlock.BlockEntity.Inventory inventory) {
- List<Integer> slots = List.<Integer>of();
- DefaultedList<Item> itemSs = this.getItems();
- for (ItemStack i : inventory.stream().toList())
- if (itemSs.contains(i.getItem()))
- slots.add(Integer.valueOf(inventory.stream().toList().indexOf(i)));
- if (slots.size() != itemSs.size()) return false;
- for (Integer i : slots)
- inventory.removeStack(i.toString(), this.items.get(itemSs.indexOf(inventory.getItem(i.intValue()).getItem())).getCount());
- ItemStack resultCopy = this.result.copy();
- for (ItemStack i : inventory.stream().toList()) {
- if (i.getItem() == resultCopy.getItem()) {
- if (i.getCount() + resultCopy.getCount() <= i.getMaxStackSize()) {
- inventory.setItem(inventory.stream().toList().indexOf(i), i.grow(resultCopy.getCount()));
- } else {
- resultCopy.setCount(i.getCount() + resultCopy.getCount() - i.getMaxStackSize());
- inventory.setItem(inventory.stream().toList().indexOf(i), i.setCount(i.getMaxStackSize()));
- }
- }
- }
- if (resultCopy.getCount() != 0) Block.dropStack(inventory.getOwner().getWorld(), inventory.getOwner().getPos(), Direction.UP, resultCopy);
- return true;
- }
- }
Add Comment
Please, Sign In to add comment