Skip to content

Commit

Permalink
fix shiftclicking craft bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Tfarcenim committed Jul 24, 2020
1 parent c0f86f4 commit b1d9cf5
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 16 deletions.
3 changes: 3 additions & 0 deletions src/main/java/io/github/alloffabric/artis/Artis.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
public class Artis implements ModInitializer {
public static final String MODID = "artis";

public static final Identifier recipe_sync = new Identifier(MODID,"sync_recipe");
public static final Identifier dummy = new Identifier("null","null");

public static final Logger logger = LogManager.getLogger();

public static final ArrayList<ArtisTableBlock> ARTIS_TABLE_BLOCKS = new ArrayList<>();
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/io/github/alloffabric/artis/ArtisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.CraftingResultInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.screen.CraftingScreenHandler;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -75,6 +84,33 @@ public void onInitializeClient() {
assets.addItemModel(id, ITEM_MODELS.get(id));
}
});

ClientSidePacketRegistry.INSTANCE.register(Artis.recipe_sync,
(packetContext, attachedData) -> {
Identifier location = attachedData.readIdentifier();
packetContext.getTaskQueue().execute(() -> {
ScreenHandler container = packetContext.getPlayer().currentScreenHandler;
if (container instanceof ArtisCraftingController) {
Recipe<?> r = MinecraftClient.getInstance().world.getRecipeManager().get(location).orElse(null);
updateLastRecipe((ArtisCraftingController) packetContext.getPlayer().currentScreenHandler, (Recipe<CraftingInventory>) r);
}
});
});

}

public static void updateLastRecipe(ArtisCraftingController container, Recipe<CraftingInventory> rec) {

CraftingInventory craftInput = container.getCraftInv();
CraftingResultInventory craftResult = container.getResultInv();

if (craftInput == null) {
System.out.println("why are these null?");
} else {
craftResult.setLastRecipe(rec);
if (rec != null) craftResult.setStack(0, rec.craft(craftInput));
else craftResult.setStack(0, ItemStack.EMPTY);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.*;
import io.github.cottonmc.cotton.gui.widget.data.HorizontalAlignment;
import io.netty.buffer.Unpooled;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.CraftingResultInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.recipe.CraftingRecipe;
import net.minecraft.recipe.Recipe;
Expand Down Expand Up @@ -124,6 +127,10 @@ public ArtisCraftingInventory getCraftInv() {
return craftInv;
}

public CraftingResultInventory getResultInv() {
return resultInv;
}

public PlayerEntity getPlayer() {
return player;
}
Expand Down Expand Up @@ -188,6 +195,7 @@ public int getCraftingSlotCount() {
return getTableType().getWidth() * getTableType().getHeight();
}

//leaving here in case it's needed
public void updateResult(int syncId, World world, PlayerEntity player, CraftingInventory craftInv, CraftingResultInventory resultInv) {
if (!world.isClient) {
ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player;
Expand Down Expand Up @@ -224,11 +232,40 @@ public void updateResult(int syncId, World world, PlayerEntity player, CraftingI
}
}

//like vanilla, but not a pile of lag
public static void updateResult(World world, PlayerEntity player, CraftingInventory inv, CraftingResultInventory result, ArtisTableType artisTableType) {
if (!world.isClient) {

ItemStack itemstack = ItemStack.EMPTY;

boolean isArtis = false;

Recipe<CraftingInventory> recipe = (Recipe<CraftingInventory>) result.getLastRecipe();
//find artis recipe first
if (recipe == null || !recipe.matches(inv, world)) {
recipe = findArtisRecipe(artisTableType,inv, world);
if (recipe != null) isArtis = true;
}
//else fall back to vanilla
if (recipe == null && artisTableType.shouldIncludeNormalRecipes()) {
recipe = findVanillaRecipe(inv,world);
}
//there is no matching recipe
if (recipe != null) {
itemstack = recipe.craft(inv);
}

result.setStack(0, itemstack);
PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer());
buf.writeIdentifier(recipe != null ? recipe.getId(): Artis.dummy);
ServerSidePacketRegistry.INSTANCE.sendToPlayer(player, Artis.recipe_sync, buf);
result.setLastRecipe(recipe);
}
}

@Override
public void onContentChanged(Inventory inv) {
this.context.run((world, pos) -> {
updateResult(this.syncId, world, this.player, this.craftInv, this.resultInv);
});
updateResult(world,player,craftInv,resultInv,tableType);
}

@Override
Expand All @@ -241,18 +278,12 @@ public ItemStack transferSlot(PlayerEntity player, int slotIndex) {
ItemStack stack = ItemStack.EMPTY;
Slot slot = this.slots.get(slotIndex);
if (slot != null && slot.hasStack()) {
ItemStack toTake = slot.getStack();
stack = toTake.copy();
if (slotIndex == getCraftingResultSlotIndex()) {
this.context.run((world, pos) -> {
toTake.getItem().onCraft(toTake, world, player);
});
if (!this.insertItem(toTake, 0, 35, true)) {
return ItemStack.EMPTY;
}

slot.onStackChanged(toTake, stack);
int slotcount = getCraftingSlotCount() + (tableType.hasCatalystSlot() ? 1 : 0);
return handleShiftCraft(player,this,slot,craftInv,resultInv,slotcount,slotcount + 36);
}
ItemStack toTake = slot.getStack();
stack = toTake.copy();

if (toTake.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
Expand Down Expand Up @@ -297,4 +328,50 @@ public void clearCraftingSlots() {
public boolean canInsertIntoSlot(ItemStack stack, Slot slot) {
return slot.inventory != this.resultInv && super.canInsertIntoSlot(stack, slot);
}

public static ItemStack handleShiftCraft(PlayerEntity player, ArtisCraftingController container, Slot resultSlot, ArtisCraftingInventory input, CraftingResultInventory craftResult, int outStart, int outEnd) {
ItemStack outputCopy = ItemStack.EMPTY;
input.setCheckMatrixChanges(false);
if (resultSlot != null && resultSlot.hasStack()) {

Recipe<CraftingInventory> recipe = (Recipe<CraftingInventory>) craftResult.getLastRecipe();
if (recipe == null && container.tableType.shouldIncludeNormalRecipes()) {
recipe = findVanillaRecipe(input,player.world);
}
while (recipe != null && recipe.matches(input, player.world)) {
ItemStack recipeOutput = resultSlot.getStack().copy();
outputCopy = recipeOutput.copy();

recipeOutput.getItem().onCraft(recipeOutput, player.world, player);

if (!player.world.isClient && !container.insertItem(recipeOutput, outStart, outEnd,true)) {
input.setCheckMatrixChanges(true);
return ItemStack.EMPTY;
}

resultSlot.onStackChanged(recipeOutput, outputCopy);
resultSlot.markDirty();

if (!player.world.isClient && recipeOutput.getCount() == outputCopy.getCount()) {
input.setCheckMatrixChanges(true);
return ItemStack.EMPTY;
}

ItemStack itemstack2 = resultSlot.onTakeItem(player, recipeOutput);
player.dropItem(itemstack2, false);
}
input.setCheckMatrixChanges(true);
updateResult(player.world, player, input, craftResult, container.tableType);
}
input.setCheckMatrixChanges(true);
return craftResult.getLastRecipe() == null ? ItemStack.EMPTY : outputCopy;
}

public static Recipe<CraftingInventory> findArtisRecipe(ArtisTableType tableType, CraftingInventory inv, World world) {
return (Recipe<CraftingInventory>) world.getRecipeManager().getFirstMatch(tableType, inv, world).orElse(null);
}

public static Recipe<CraftingInventory> findVanillaRecipe(CraftingInventory inv, World world) {
return world.getRecipeManager().getFirstMatch(RecipeType.CRAFTING, inv, world).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
public class ArtisCraftingInventory extends CraftingInventory {
private final DefaultedList<ItemStack> stacks;
private final ArtisCraftingController container;
private boolean checkMatrixChanges = true;

public ArtisCraftingInventory(ArtisCraftingController container, int width, int height) {
super(container, width, height);
Expand Down Expand Up @@ -48,16 +49,15 @@ public ItemStack removeStack(int slot) {
public ItemStack removeStack(int slot, int amount) {
ItemStack stack = Inventories.splitStack(this.stacks, slot, amount);
if (!stack.isEmpty()) {
this.container.onContentChanged(this);
if (checkMatrixChanges)this.container.onContentChanged(this);
}

return stack;
}

@Override
public void setStack(int slot, ItemStack stack) {
this.stacks.set(slot, stack);
this.container.onContentChanged(this);
if (checkMatrixChanges)this.container.onContentChanged(this);
}

@Override
Expand Down Expand Up @@ -94,4 +94,8 @@ public boolean shouldCompareCatalyst() {
public PlayerEntity getPlayer() {
return container.getPlayer();
}

public void setCheckMatrixChanges(boolean b) {
this.checkMatrixChanges = b;
}
}

0 comments on commit b1d9cf5

Please sign in to comment.