diff --git a/src/main/java/carpet_autocraftingtable/AutoCraftingTable.java b/src/main/java/carpet_autocraftingtable/AutoCraftingTable.java index d685a2b..8889aca 100644 --- a/src/main/java/carpet_autocraftingtable/AutoCraftingTable.java +++ b/src/main/java/carpet_autocraftingtable/AutoCraftingTable.java @@ -6,14 +6,12 @@ public class AutoCraftingTable implements CarpetExtension { public static void noop() { } - static - { + static { CarpetServer.manageExtension(new AutoCraftingTable()); } @Override - public void onGameStarted() - { + public void onGameStarted() { CraftingTableBlockEntity.init(); // let's /carpet handle our few simple settings CarpetServer.settingsManager.parseSettingsClass(AutoCraftingTableSettings.class); diff --git a/src/main/java/carpet_autocraftingtable/AutoCraftingTableContainer.java b/src/main/java/carpet_autocraftingtable/AutoCraftingTableContainer.java index 58b7385..e0d45ce 100644 --- a/src/main/java/carpet_autocraftingtable/AutoCraftingTableContainer.java +++ b/src/main/java/carpet_autocraftingtable/AutoCraftingTableContainer.java @@ -26,14 +26,9 @@ public class AutoCraftingTableContainer extends AbstractRecipeScreenHandler 0) { - stack.onCraft(this.player.world, this.player, amount); - } - - if (this.inventory instanceof RecipeUnlocker) { - ((RecipeUnlocker)this.inventory).unlockLastRecipe(this.player); - } + protected void onCrafted(ItemStack stack, int amount) { + super.onCrafted(stack); // from CraftingResultsSlot onCrafted + if (amount > 0) stack.onCraft(this.player.world, this.player, amount); + if (this.inventory instanceof RecipeUnlocker) ((RecipeUnlocker)this.inventory).unlockLastRecipe(this.player); } @Override - public void onTakeItem(PlayerEntity player, ItemStack stack) - { + public void onTakeItem(PlayerEntity player, ItemStack stack) { onCrafted(stack, stack.getCount()); super.onTakeItem(player, stack); } diff --git a/src/main/java/carpet_autocraftingtable/CraftingTableBlockEntity.java b/src/main/java/carpet_autocraftingtable/CraftingTableBlockEntity.java index a02c980..4b747e6 100644 --- a/src/main/java/carpet_autocraftingtable/CraftingTableBlockEntity.java +++ b/src/main/java/carpet_autocraftingtable/CraftingTableBlockEntity.java @@ -31,6 +31,8 @@ import java.util.List; import java.util.Optional; +import static net.minecraft.util.math.Direction.DOWN; + public class CraftingTableBlockEntity extends LockableContainerBlockEntity implements SidedInventory, RecipeUnlocker, RecipeInputProvider { public static final BlockEntityType TYPE = Registry.register( Registry.BLOCK_ENTITY_TYPE, @@ -82,8 +84,7 @@ protected ScreenHandler createScreenHandler(int id, PlayerInventory playerInvent @Override public int[] getAvailableSlots(Direction dir) { - if (dir == Direction.DOWN && (!output.isEmpty() || getCurrentRecipe().isPresent())) return OUTPUT_SLOTS; - return INPUT_SLOTS; + return (dir == DOWN && (!output.isEmpty() || getCurrentRecipe().isPresent())) ? OUTPUT_SLOTS : INPUT_SLOTS; } @Override @@ -93,8 +94,7 @@ public boolean canInsert(int slot, ItemStack stack, Direction dir) { @Override public boolean canExtract(int slot, ItemStack stack, Direction dir) { - if (slot == 0) return !output.isEmpty() || getCurrentRecipe().isPresent(); - return true; + return slot != 0 || !output.isEmpty() || getCurrentRecipe().isPresent(); } @Override @@ -126,9 +126,7 @@ public ItemStack getStack(int slot) { @Override public ItemStack removeStack(int slot, int amount) { if (slot == 0) { - if (output.isEmpty()) { - output = craft(); - } + if (output.isEmpty()) output = craft(); return output.split(amount); } return Inventories.splitStack(this.inventory, slot - 1, amount); @@ -146,11 +144,8 @@ public ItemStack removeStack(int slot) { @Override public void setStack(int slot, ItemStack stack) { - if (slot == 0) { - output = stack; - return; - } - inventory.set(slot - 1, stack); + if (slot == 0) output = stack; + else inventory.set(slot - 1, stack); } @Override @@ -166,9 +161,7 @@ public boolean canPlayerUse(PlayerEntity var1) { @Override public void provideRecipeInputs(RecipeMatcher finder) { - for (ItemStack stack : this.inventory) { - finder.addInput(stack); - } + for (ItemStack stack : this.inventory) finder.addInput(stack); } @Override @@ -203,9 +196,7 @@ private ItemStack craft() { for (int i = 0; i < 9; i++) { ItemStack current = inventory.get(i); ItemStack remainingStack = remaining.get(i); - if (!current.isEmpty()) { - current.decrement(1); - } + if (!current.isEmpty()) current.decrement(1); if (!remainingStack.isEmpty()) { if (current.isEmpty()) { inventory.set(i, remainingStack); diff --git a/src/main/java/carpet_autocraftingtable/mixins/AbstractBlockMixin.java b/src/main/java/carpet_autocraftingtable/mixins/AbstractBlockMixin.java deleted file mode 100644 index 6dcc9eb..0000000 --- a/src/main/java/carpet_autocraftingtable/mixins/AbstractBlockMixin.java +++ /dev/null @@ -1,17 +0,0 @@ -package carpet_autocraftingtable.mixins; - -import carpet_autocraftingtable.AutoCraftingTableSettings; -import net.minecraft.block.AbstractBlock; -import net.minecraft.block.BlockEntityProvider; -import net.minecraft.block.CraftingTableBlock; -import org.spongepowered.asm.mixin.Mixin; - -@Mixin(AbstractBlock.class) -public class AbstractBlockMixin -{ - - public boolean hasBlockEntity() { - if ((Object) this instanceof CraftingTableBlock) return AutoCraftingTableSettings.autoCraftingTable; - return this instanceof BlockEntityProvider; - } -} diff --git a/src/main/java/carpet_autocraftingtable/mixins/CraftingInventoryMixin.java b/src/main/java/carpet_autocraftingtable/mixins/CraftingInventoryMixin.java index 9963f5f..d681e29 100644 --- a/src/main/java/carpet_autocraftingtable/mixins/CraftingInventoryMixin.java +++ b/src/main/java/carpet_autocraftingtable/mixins/CraftingInventoryMixin.java @@ -8,8 +8,7 @@ import org.spongepowered.asm.mixin.gen.Accessor; @Mixin(CraftingInventory.class) -public interface CraftingInventoryMixin -{ +public interface CraftingInventoryMixin { @Mutable @Accessor("stacks") void setInventory(DefaultedList inventory); diff --git a/src/main/java/carpet_autocraftingtable/mixins/CraftingTableBlockMixin.java b/src/main/java/carpet_autocraftingtable/mixins/CraftingTableBlockMixin.java index 560fcf8..d60d271 100644 --- a/src/main/java/carpet_autocraftingtable/mixins/CraftingTableBlockMixin.java +++ b/src/main/java/carpet_autocraftingtable/mixins/CraftingTableBlockMixin.java @@ -26,22 +26,15 @@ protected CraftingTableBlockMixin(Settings block$Settings_1) { } @Override - public BlockEntity createBlockEntity(BlockPos pos, BlockState state) - { + public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { return (AutoCraftingTableSettings.autoCraftingTable && state.isOf(Blocks.CRAFTING_TABLE)) ? new CraftingTableBlockEntity(pos, state) : null; } @Inject(method = "createScreenHandlerFactory", at = @At("HEAD"), cancellable = true) - private void onCreateScreenHandler(BlockState state, World world, BlockPos pos, CallbackInfoReturnable cir) - { - if (!AutoCraftingTableSettings.autoCraftingTable) return; - if (!state.hasBlockEntity()) return; - if (!world.isClient) { - BlockEntity blockEntity = world.getBlockEntity(pos); - if (blockEntity instanceof CraftingTableBlockEntity) { - cir.setReturnValue((NamedScreenHandlerFactory) blockEntity); - } - } + private void onCreateScreenHandler(BlockState state, World world, BlockPos pos, CallbackInfoReturnable cir) { + if (!AutoCraftingTableSettings.autoCraftingTable || !state.hasBlockEntity() || world.isClient) return; + BlockEntity blockEntity = world.getBlockEntity(pos); + if (blockEntity instanceof CraftingTableBlockEntity) cir.setReturnValue((NamedScreenHandlerFactory) blockEntity); } @Override @@ -51,11 +44,9 @@ public boolean hasComparatorOutput(BlockState state) { @Override public int getComparatorOutput(BlockState state, World world, BlockPos pos) { - if (!AutoCraftingTableSettings.autoCraftingTable) return 0; - if (!state.hasBlockEntity()) return 0; + if (!AutoCraftingTableSettings.autoCraftingTable || !state.hasBlockEntity()) return 0; BlockEntity blockEntity = world.getBlockEntity(pos); - if (blockEntity instanceof CraftingTableBlockEntity) { - CraftingTableBlockEntity craftingTableBlockEntity = (CraftingTableBlockEntity) blockEntity; + if (blockEntity instanceof CraftingTableBlockEntity craftingTableBlockEntity) { int filled = 0; for (ItemStack stack : craftingTableBlockEntity.inventory) { if (!stack.isEmpty()) filled++; @@ -70,8 +61,7 @@ public int getComparatorOutput(BlockState state, World world, BlockPos pos) { public void onStateReplaced(BlockState oldState, World world, BlockPos pos, BlockState newState, boolean moved) { if (oldState.getBlock() != newState.getBlock()) { BlockEntity blockEntity = world.getBlockEntity(pos); - if (blockEntity instanceof CraftingTableBlockEntity) { - CraftingTableBlockEntity craftingTableBlockEntity = ((CraftingTableBlockEntity)blockEntity); + if (blockEntity instanceof CraftingTableBlockEntity craftingTableBlockEntity) { ItemScatterer.spawn(world, pos, craftingTableBlockEntity.inventory); if (!craftingTableBlockEntity.output.isEmpty()) { ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), craftingTableBlockEntity.output); @@ -79,7 +69,6 @@ public void onStateReplaced(BlockState oldState, World world, BlockPos pos, Bloc world.updateNeighborsAlways(pos, this); } world.removeBlockEntity(pos); - super.onStateReplaced(oldState, world, pos, newState, moved); } } @@ -87,8 +76,7 @@ public void onStateReplaced(BlockState oldState, World world, BlockPos pos, Bloc @Override public void onDestroyedByExplosion(World world, BlockPos pos, Explosion explosion) { BlockEntity blockEntity = world.getBlockEntity(pos); - if (blockEntity instanceof CraftingTableBlockEntity) { - CraftingTableBlockEntity craftingTableBlockEntity = ((CraftingTableBlockEntity) blockEntity); + if (blockEntity instanceof CraftingTableBlockEntity craftingTableBlockEntity) { ItemScatterer.spawn(world, pos, craftingTableBlockEntity.inventory); if (!craftingTableBlockEntity.output.isEmpty()) { ItemScatterer.spawn(world, pos.getX(), pos.getY(), pos.getZ(), craftingTableBlockEntity.output); diff --git a/src/main/java/carpet_autocraftingtable/mixins/CrashReport_noopMixin.java b/src/main/java/carpet_autocraftingtable/mixins/CrashReport_noopMixin.java index 500b026..af56b0e 100644 --- a/src/main/java/carpet_autocraftingtable/mixins/CrashReport_noopMixin.java +++ b/src/main/java/carpet_autocraftingtable/mixins/CrashReport_noopMixin.java @@ -8,8 +8,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(CrashReport.class) -public class CrashReport_noopMixin -{ +public class CrashReport_noopMixin { @Inject(method = "initCrashReport", at = @At("HEAD")) private static void gameStarted(CallbackInfo ci) { diff --git a/src/main/resources/carpet-autocraftingtable.mixins.json b/src/main/resources/carpet-autocraftingtable.mixins.json index 66f7eb1..98b0b22 100644 --- a/src/main/resources/carpet-autocraftingtable.mixins.json +++ b/src/main/resources/carpet-autocraftingtable.mixins.json @@ -5,8 +5,7 @@ "mixins": [ "CrashReport_noopMixin", "CraftingInventoryMixin", - "CraftingTableBlockMixin", - "AbstractBlockMixin" + "CraftingTableBlockMixin" ], "injectors": { "defaultRequire": 1