[Fix] EndStone Smelter destroys result item when recipe is clicked in book

This commit is contained in:
Frank 2022-07-10 22:42:22 +02:00
parent 3694bf1eee
commit f6df2a2504
5 changed files with 83 additions and 64 deletions

View file

@ -2,7 +2,7 @@ package org.betterx.betterend.blocks.entities;
import org.betterx.betterend.BetterEnd; import org.betterx.betterend.BetterEnd;
import org.betterx.betterend.blocks.EndStoneSmelter; import org.betterx.betterend.blocks.EndStoneSmelter;
import org.betterx.betterend.client.gui.EndStoneSmelterScreenHandler; import org.betterx.betterend.client.gui.EndStoneSmelterMenu;
import org.betterx.betterend.recipe.builders.AlloyingRecipe; import org.betterx.betterend.recipe.builders.AlloyingRecipe;
import org.betterx.betterend.registry.EndBlockEntities; import org.betterx.betterend.registry.EndBlockEntities;
@ -49,9 +49,15 @@ import java.util.Map;
public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer, RecipeHolder, StackedContentsCompatible { public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity implements WorldlyContainer, RecipeHolder, StackedContentsCompatible {
private static final int[] TOP_SLOTS = new int[]{0, 1}; private static final int[] TOP_SLOTS = new int[]{
private static final int[] BOTTOM_SLOTS = new int[]{2, 3}; EndStoneSmelterMenu.INGREDIENT_SLOT_A,
private static final int[] SIDE_SLOTS = new int[]{1, 2}; EndStoneSmelterMenu.INGREDIENT_SLOT_B
};
private static final int[] BOTTOM_SLOTS = new int[]{EndStoneSmelterMenu.FUEL_SLOT, EndStoneSmelterMenu.RESULT_SLOT};
private static final int[] SIDE_SLOTS = new int[]{
EndStoneSmelterMenu.INGREDIENT_SLOT_B,
EndStoneSmelterMenu.FUEL_SLOT
};
private static final Map<Item, Integer> AVAILABLE_FUELS = Maps.newHashMap(); private static final Map<Item, Integer> AVAILABLE_FUELS = Maps.newHashMap();
private final Object2IntOpenHashMap<ResourceLocation> recipesUsed; private final Object2IntOpenHashMap<ResourceLocation> recipesUsed;
@ -65,7 +71,7 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
public EndStoneSmelterBlockEntity(BlockPos blockPos, BlockState blockState) { public EndStoneSmelterBlockEntity(BlockPos blockPos, BlockState blockState) {
super(EndBlockEntities.END_STONE_SMELTER, blockPos, blockState); super(EndBlockEntities.END_STONE_SMELTER, blockPos, blockState);
this.inventory = NonNullList.withSize(4, ItemStack.EMPTY); this.inventory = NonNullList.withSize(EndStoneSmelterMenu.SLOT_COUNT, ItemStack.EMPTY);
this.recipesUsed = new Object2IntOpenHashMap<>(); this.recipesUsed = new Object2IntOpenHashMap<>();
this.propertyDelegate = new ContainerData() { this.propertyDelegate = new ContainerData() {
public int get(int index) { public int get(int index) {
@ -151,7 +157,7 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
if (stack.getCount() > getMaxStackSize()) { if (stack.getCount() > getMaxStackSize()) {
stack.setCount(getMaxStackSize()); stack.setCount(getMaxStackSize());
} }
if ((slot == 0 || slot == 1) && !stackValid) { if ((slot == EndStoneSmelterMenu.INGREDIENT_SLOT_A || slot == EndStoneSmelterMenu.INGREDIENT_SLOT_B) && !stackValid) {
smeltTimeTotal = getSmeltTime(); smeltTimeTotal = getSmeltTime();
smeltTime = 0; smeltTime = 0;
setChanged(); setChanged();
@ -231,7 +237,7 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
@Override @Override
protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) { protected AbstractContainerMenu createMenu(int syncId, Inventory playerInventory) {
return new EndStoneSmelterScreenHandler(syncId, playerInventory, this, propertyDelegate); return new EndStoneSmelterMenu(syncId, playerInventory, this, propertyDelegate);
} }
public static void tick( public static void tick(
@ -249,9 +255,11 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
boolean burning = initialBurning; boolean burning = initialBurning;
if (!tickLevel.isClientSide) { if (!tickLevel.isClientSide) {
ItemStack fuel = blockEntity.inventory.get(2); ItemStack fuel = blockEntity.inventory.get(EndStoneSmelterMenu.FUEL_SLOT);
if (!burning && (fuel.isEmpty() || blockEntity.inventory.get(0).isEmpty() && blockEntity.inventory.get(1) if (!burning && (fuel.isEmpty()
.isEmpty())) { || blockEntity.inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_A).isEmpty()
&& blockEntity.inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_B).isEmpty())
) {
if (blockEntity.smeltTime > 0) { if (blockEntity.smeltTime > 0) {
blockEntity.smeltTime = Mth.clamp(blockEntity.smeltTime - 2, 0, blockEntity.smeltTimeTotal); blockEntity.smeltTime = Mth.clamp(blockEntity.smeltTime - 2, 0, blockEntity.smeltTimeTotal);
} }
@ -276,7 +284,7 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
if (fuel.isEmpty()) { if (fuel.isEmpty()) {
Item remainFuel = item.getCraftingRemainingItem(); Item remainFuel = item.getCraftingRemainingItem();
blockEntity.inventory.set( blockEntity.inventory.set(
2, EndStoneSmelterMenu.FUEL_SLOT,
remainFuel == null ? ItemStack.EMPTY : new ItemStack(remainFuel) remainFuel == null ? ItemStack.EMPTY : new ItemStack(remainFuel)
); );
} }
@ -309,16 +317,18 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
if (recipe == null) return false; if (recipe == null) return false;
boolean validInput; boolean validInput;
if (recipe instanceof AlloyingRecipe) { if (recipe instanceof AlloyingRecipe) {
validInput = !inventory.get(0).isEmpty() && !inventory.get(1).isEmpty(); validInput = !inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_A).isEmpty()
&& !inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_B).isEmpty();
} else { } else {
validInput = !inventory.get(0).isEmpty() || !inventory.get(1).isEmpty(); validInput = !inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_A).isEmpty()
|| !inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_B).isEmpty();
} }
if (validInput) { if (validInput) {
ItemStack result = recipe.getResultItem(); ItemStack result = recipe.getResultItem();
if (result.isEmpty()) { if (result.isEmpty()) {
return false; return false;
} }
ItemStack output = this.inventory.get(3); ItemStack output = this.inventory.get(EndStoneSmelterMenu.RESULT_SLOT);
int outCount = output.getCount(); int outCount = output.getCount();
int total = outCount + result.getCount(); int total = outCount + result.getCount();
if (output.isEmpty()) { if (output.isEmpty()) {
@ -339,9 +349,9 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
if (recipe == null || !canAcceptRecipeOutput(recipe)) return; if (recipe == null || !canAcceptRecipeOutput(recipe)) return;
ItemStack result = recipe.getResultItem(); ItemStack result = recipe.getResultItem();
ItemStack output = inventory.get(3); ItemStack output = inventory.get(EndStoneSmelterMenu.RESULT_SLOT);
if (output.isEmpty()) { if (output.isEmpty()) {
inventory.set(3, result.copy()); inventory.set(EndStoneSmelterMenu.RESULT_SLOT, result.copy());
} else if (output.getItem() == result.getItem()) { } else if (output.getItem() == result.getItem()) {
output.grow(result.getCount()); output.grow(result.getCount());
} }
@ -352,13 +362,13 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
} }
if (recipe instanceof AlloyingRecipe) { if (recipe instanceof AlloyingRecipe) {
inventory.get(0).shrink(1); inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_A).shrink(1);
inventory.get(1).shrink(1); inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_B).shrink(1);
} else { } else {
if (!inventory.get(0).isEmpty()) { if (!inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_A).isEmpty()) {
inventory.get(0).shrink(1); inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_A).shrink(1);
} else { } else {
inventory.get(1).shrink(1); inventory.get(EndStoneSmelterMenu.INGREDIENT_SLOT_B).shrink(1);
} }
} }
} }
@ -399,7 +409,7 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
@Override @Override
public boolean canTakeItemThroughFace(int slot, ItemStack stack, Direction dir) { public boolean canTakeItemThroughFace(int slot, ItemStack stack, Direction dir) {
if (dir == Direction.DOWN && slot == 2) { if (dir == Direction.DOWN && slot == EndStoneSmelterMenu.FUEL_SLOT) {
return stack.getItem() == Items.BUCKET; return stack.getItem() == Items.BUCKET;
} }
return true; return true;
@ -443,12 +453,12 @@ public class EndStoneSmelterBlockEntity extends BaseContainerBlockEntity impleme
} }
public boolean canPlaceItem(int slot, ItemStack stack) { public boolean canPlaceItem(int slot, ItemStack stack) {
if (slot == 3) { if (slot == EndStoneSmelterMenu.RESULT_SLOT) {
return false; return false;
} else if (slot != 2) { } else if (slot != EndStoneSmelterMenu.FUEL_SLOT) {
return true; return true;
} }
ItemStack itemStack = this.inventory.get(2); ItemStack itemStack = this.inventory.get(EndStoneSmelterMenu.FUEL_SLOT);
return canUseAsFuel(stack) || stack.getItem() == Items.BUCKET && itemStack.getItem() != Items.BUCKET; return canUseAsFuel(stack) || stack.getItem() == Items.BUCKET && itemStack.getItem() != Items.BUCKET;
} }

View file

@ -4,7 +4,7 @@ import org.betterx.bclib.BCLib;
import org.betterx.bclib.util.TranslationHelper; import org.betterx.bclib.util.TranslationHelper;
import org.betterx.betterend.BetterEnd; import org.betterx.betterend.BetterEnd;
import org.betterx.betterend.blocks.EndStoneSmelter; import org.betterx.betterend.blocks.EndStoneSmelter;
import org.betterx.betterend.client.gui.EndStoneSmelterScreenHandler; import org.betterx.betterend.client.gui.EndStoneSmelterMenu;
import org.betterx.betterend.client.render.BetterEndSkyRenderer; import org.betterx.betterend.client.render.BetterEndSkyRenderer;
import org.betterx.betterend.events.ItemTooltipCallback; import org.betterx.betterend.events.ItemTooltipCallback;
import org.betterx.betterend.interfaces.MultiModelItem; import org.betterx.betterend.interfaces.MultiModelItem;
@ -26,9 +26,9 @@ import net.fabricmc.fabric.api.client.rendering.v1.DimensionRenderingRegistry;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry; import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
public class BetterEndClient implements ClientModInitializer { public class BetterEndClient implements ClientModInitializer {
public final static MenuType<EndStoneSmelterScreenHandler> HANDLER_TYPE = ScreenHandlerRegistry.registerSimple( public final static MenuType<EndStoneSmelterMenu> HANDLER_TYPE = ScreenHandlerRegistry.registerSimple(
BetterEnd.makeID(EndStoneSmelter.ID), BetterEnd.makeID(EndStoneSmelter.ID),
EndStoneSmelterScreenHandler::new EndStoneSmelterMenu::new
); );
@Override @Override

View file

@ -23,17 +23,27 @@ import org.anti_ad.mc.ipn.api.IPNIgnore;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
@IPNIgnore @IPNIgnore
public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> { public class EndStoneSmelterMenu extends RecipeBookMenu<Container> {
public static final int INGREDIENT_SLOT_A = 0;
public static final int INGREDIENT_SLOT_B = 1;
public static final int FUEL_SLOT = 2;
public static final int RESULT_SLOT = 3;
public static final int SLOT_COUNT = 4;
private static final int INV_SLOT_START = SLOT_COUNT;
private static final int INV_SLOT_END = INV_SLOT_START + 3 * 9;
private static final int USE_ROW_SLOT_START = INV_SLOT_END;
private static final int USE_ROW_SLOT_END = USE_ROW_SLOT_START + 9;
private final Container inventory; private final Container inventory;
private final ContainerData propertyDelegate; private final ContainerData propertyDelegate;
protected final Level world; protected final Level world;
public EndStoneSmelterScreenHandler(int syncId, Inventory playerInventory) { public EndStoneSmelterMenu(int syncId, Inventory playerInventory) {
this(syncId, playerInventory, new SimpleContainer(4), new SimpleContainerData(4)); this(syncId, playerInventory, new SimpleContainer(SLOT_COUNT), new SimpleContainerData(4));
} }
public EndStoneSmelterScreenHandler( public EndStoneSmelterMenu(
int syncId, int syncId,
Inventory playerInventory, Inventory playerInventory,
Container inventory, Container inventory,
@ -45,10 +55,10 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
this.world = playerInventory.player.level; this.world = playerInventory.player.level;
addDataSlots(propertyDelegate); addDataSlots(propertyDelegate);
addSlot(new Slot(inventory, 0, 45, 17)); addSlot(new Slot(inventory, INGREDIENT_SLOT_A, 45, 17));
addSlot(new Slot(inventory, 1, 67, 17)); addSlot(new Slot(inventory, INGREDIENT_SLOT_B, 67, 17));
addSlot(new SmelterFuelSlot(this, inventory, 2, 56, 53)); addSlot(new SmelterFuelSlot(this, inventory, FUEL_SLOT, 56, 53));
addSlot(new SmelterOutputSlot(playerInventory.player, inventory, 3, 129, 35)); addSlot(new SmelterOutputSlot(playerInventory.player, inventory, RESULT_SLOT, 129, 35));
for (int i = 0; i < 3; ++i) { for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 9; ++j) { for (int j = 0; j < 9; ++j) {
@ -74,7 +84,9 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
@Override @Override
public void clearCraftingContent() { public void clearCraftingContent() {
inventory.clearContent(); this.getSlot(INGREDIENT_SLOT_A).set(ItemStack.EMPTY);
this.getSlot(INGREDIENT_SLOT_B).set(ItemStack.EMPTY);
this.getSlot(RESULT_SLOT).set(ItemStack.EMPTY);
} }
@Override @Override
@ -84,7 +96,7 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
@Override @Override
public int getResultSlotIndex() { public int getResultSlotIndex() {
return 3; return RESULT_SLOT;
} }
@Override @Override
@ -99,7 +111,7 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
@Override @Override
public int getSize() { public int getSize() {
return 4; return SLOT_COUNT;
} }
@Override @Override
@ -109,7 +121,7 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
@Override @Override
public boolean shouldMoveToInventory(int i) { public boolean shouldMoveToInventory(int i) {
return i != this.getResultSlotIndex(); return i != FUEL_SLOT;
} }
@Override @Override
@ -134,28 +146,25 @@ public class EndStoneSmelterScreenHandler extends RecipeBookMenu<Container> {
ItemStack slotStack = slot.getItem(); ItemStack slotStack = slot.getItem();
ItemStack itemStack = slotStack.copy(); ItemStack itemStack = slotStack.copy();
if (index == 3) { if (index == RESULT_SLOT) {
if (!moveItemStackTo(slotStack, 4, 40, true)) { if (!moveItemStackTo(slotStack, INV_SLOT_START, USE_ROW_SLOT_END, true)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
slot.onQuickCraft(slotStack, itemStack); slot.onQuickCraft(slotStack, itemStack);
} else if (index != 2 && index != 1 && index != 0) { } else if (((index == FUEL_SLOT) || (index == INGREDIENT_SLOT_A) || (index == INGREDIENT_SLOT_B))
if (isSmeltable(slotStack)) { ? !this.moveItemStackTo(slotStack, USE_ROW_SLOT_START, USE_ROW_SLOT_END, false)
if (!moveItemStackTo(slotStack, 0, 2, false)) { : (this.isSmeltable(slotStack)
return ItemStack.EMPTY; ? !this.moveItemStackTo(slotStack, INGREDIENT_SLOT_A, FUEL_SLOT, false)
} : (this.isFuel(slotStack)
} else if (isFuel(slotStack)) { ? !this.moveItemStackTo(slotStack, FUEL_SLOT, RESULT_SLOT, false)
if (!moveItemStackTo(slotStack, 2, 3, false)) { : (((index >= INV_SLOT_START) && (index < INV_SLOT_END))
return ItemStack.EMPTY; ? !this.moveItemStackTo(slotStack, USE_ROW_SLOT_START, USE_ROW_SLOT_END, false)
} : ((index >= USE_ROW_SLOT_START) && (index < USE_ROW_SLOT_END) && !this.moveItemStackTo(
} else if (index < 31) { slotStack,
if (!moveItemStackTo(slotStack, 31, 40, false)) { INV_SLOT_START,
return ItemStack.EMPTY; INV_SLOT_END,
} false
} else if (index < 40 && !moveItemStackTo(slotStack, 4, 31, false)) { )))))) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(slotStack, 4, 40, false)) {
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }

View file

@ -18,7 +18,7 @@ import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment; import net.fabricmc.api.Environment;
@Environment(EnvType.CLIENT) @Environment(EnvType.CLIENT)
public class EndStoneSmelterScreen extends AbstractContainerScreen<EndStoneSmelterScreenHandler> implements RecipeUpdateListener { public class EndStoneSmelterScreen extends AbstractContainerScreen<EndStoneSmelterMenu> implements RecipeUpdateListener {
private final static ResourceLocation RECIPE_BUTTON_TEXTURE = new ResourceLocation("textures/gui/recipe_button.png"); private final static ResourceLocation RECIPE_BUTTON_TEXTURE = new ResourceLocation("textures/gui/recipe_button.png");
private final static ResourceLocation BACKGROUND_TEXTURE = BetterEnd.makeID("textures/gui/smelter_gui.png"); private final static ResourceLocation BACKGROUND_TEXTURE = BetterEnd.makeID("textures/gui/smelter_gui.png");
@ -26,7 +26,7 @@ public class EndStoneSmelterScreen extends AbstractContainerScreen<EndStoneSmelt
public final EndStoneSmelterRecipeBookScreen recipeBook; public final EndStoneSmelterRecipeBookScreen recipeBook;
private boolean narrow; private boolean narrow;
public EndStoneSmelterScreen(EndStoneSmelterScreenHandler handler, Inventory inventory, Component title) { public EndStoneSmelterScreen(EndStoneSmelterMenu handler, Inventory inventory, Component title) {
super(handler, inventory, title); super(handler, inventory, title);
recipeBook = new EndStoneSmelterRecipeBookScreen(); recipeBook = new EndStoneSmelterRecipeBookScreen();
} }

View file

@ -1,6 +1,6 @@
package org.betterx.betterend.client.gui.slot; package org.betterx.betterend.client.gui.slot;
import org.betterx.betterend.client.gui.EndStoneSmelterScreenHandler; import org.betterx.betterend.client.gui.EndStoneSmelterMenu;
import net.minecraft.world.Container; import net.minecraft.world.Container;
import net.minecraft.world.inventory.FurnaceFuelSlot; import net.minecraft.world.inventory.FurnaceFuelSlot;
@ -9,9 +9,9 @@ import net.minecraft.world.item.ItemStack;
public class SmelterFuelSlot extends Slot { public class SmelterFuelSlot extends Slot {
private final EndStoneSmelterScreenHandler handler; private final EndStoneSmelterMenu handler;
public SmelterFuelSlot(EndStoneSmelterScreenHandler handler, Container inventory, int index, int x, int y) { public SmelterFuelSlot(EndStoneSmelterMenu handler, Container inventory, int index, int x, int y) {
super(inventory, index, x, y); super(inventory, index, x, y);
this.handler = handler; this.handler = handler;
} }