Skip to content

Commit

Permalink
Merge pull request #49 from fxmorin/cleanup
Browse files Browse the repository at this point in the history
Much needed cleanup.
  • Loading branch information
gnembon authored Feb 5, 2022
2 parents 16031a4 + 74401b8 commit 5f91f61
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 94 deletions.
6 changes: 2 additions & 4 deletions src/main/java/carpet_autocraftingtable/AutoCraftingTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,9 @@ public class AutoCraftingTableContainer extends AbstractRecipeScreenHandler<Craf
this.player = playerInventory.player;
this.addSlot(new OutputSlot(this.blockEntity, this.player));

for(int y = 0; y < 3; ++y) {
for(int x = 0; x < 3; ++x) {
this.addSlot(new Slot(this.blockEntity, x + y * 3 + 1, 30 + x * 18, 17 + y * 18));
}
}

for(int y = 0; y < 3; ++y) {
for(int x = 0; x < 9; ++x) {
if (x < 3) this.addSlot(new Slot(this.blockEntity, x + y * 3 + 1, 30 + x * 18, 17 + y * 18));
this.addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, 84 + y * 18));
}
}
Expand Down Expand Up @@ -111,16 +106,11 @@ public ItemStack transferSlot(PlayerEntity player, int slot) {
if (slot == 0) {
ItemStack before = this.blockEntity.getStack(0).copy();
ItemStack current = before.copy();
if (!this.insertItem(current, 10, 46, true)) {
return ItemStack.EMPTY;
}
if (!this.insertItem(current, 10, 46, true)) return ItemStack.EMPTY;
this.blockEntity.removeStack(0, before.getCount() - current.getCount());

if(player instanceof ServerPlayerEntity && blockEntity.getLastRecipe() != null)
{
// this sets recipe in container
if (!blockEntity.shouldCraftRecipe(player.world, (ServerPlayerEntity) player, blockEntity.getLastRecipe()))
{
if(player instanceof ServerPlayerEntity && blockEntity.getLastRecipe() != null) { // this sets recipe in container
if (!blockEntity.shouldCraftRecipe(player.world, (ServerPlayerEntity) player, blockEntity.getLastRecipe())) {
return ItemStack.EMPTY;
}
}
Expand All @@ -140,7 +130,7 @@ public void close(PlayerEntity player) {
}

private class OutputSlot extends Slot {
private PlayerEntity player;
private final PlayerEntity player;
OutputSlot(Inventory inv, PlayerEntity player) {
super(inv, 0, 124, 35);
this.player = player;
Expand All @@ -157,22 +147,14 @@ protected void onTake(int amount) {
}

@Override
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);
}
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CraftingTableBlockEntity> TYPE = Registry.register(
Registry.BLOCK_ENTITY_TYPE,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemStack> inventory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NamedScreenHandlerFactory> 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<NamedScreenHandlerFactory> cir) {
if (!AutoCraftingTableSettings.autoCraftingTable || !state.hasBlockEntity() || world.isClient) return;
BlockEntity blockEntity = world.getBlockEntity(pos);
if (blockEntity instanceof CraftingTableBlockEntity) cir.setReturnValue((NamedScreenHandlerFactory) blockEntity);
}

@Override
Expand All @@ -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++;
Expand All @@ -70,25 +61,22 @@ 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);
}
world.updateNeighborsAlways(pos, this);
}
world.removeBlockEntity(pos);

super.onStateReplaced(oldState, world, pos, newState, moved);
}
}

@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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/carpet-autocraftingtable.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"mixins": [
"CrashReport_noopMixin",
"CraftingInventoryMixin",
"CraftingTableBlockMixin",
"AbstractBlockMixin"
"CraftingTableBlockMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 5f91f61

Please sign in to comment.