Skip to content

Commit

Permalink
fix lag from recipe lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
GlodBlock committed Aug 10, 2023
1 parent 4302dad commit 34be221
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
import appeng.container.SlotSemantic;
import appeng.container.slot.FakeSlot;
import appeng.core.localization.GuiText;
import appeng.util.item.AEItemStack;
import com.glodblock.github.client.button.BlitMap;
import com.glodblock.github.client.button.FCToggleButton;
import com.glodblock.github.client.container.ContainerFluidPatternTerminal;
import com.glodblock.github.client.slot.SlotSingleItem;
import com.glodblock.github.common.item.ItemFluidPacket;
import com.glodblock.github.network.NetworkManager;
import com.glodblock.github.network.packets.CPacketFluidCraftBtns;
Expand All @@ -27,8 +25,6 @@
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.ITextComponent;

import javax.annotation.Nonnull;

public class GuiFluidPatternTerminal extends ItemTerminalScreen<ContainerFluidPatternTerminal> {
private static final String MODES_TEXTURE = "guis/pattern_modes.png";
private static final Blitter CRAFTING_MODE_BG = Blitter.texture(MODES_TEXTURE).src(0, 0, 126, 68);
Expand Down Expand Up @@ -143,21 +139,11 @@ private void clear() {

@Override
protected void moveItems(MatrixStack matrices, Slot slot) {
if (!(slot instanceof FakeSlot && (FluidRenderUtils.renderFluidPacketIntoGuiSlot(
slot, slot.getStack(), stackSizeRenderer, font) || renderMEStyleSlot(slot, slot.getStack(), matrices)))) {
super.moveItems(matrices, slot);
}
}

private boolean renderMEStyleSlot(Slot slot, @Nonnull ItemStack stack, MatrixStack matrices) {
if (slot instanceof FakeSlot && !stack.isEmpty() && !(stack.getItem() instanceof ItemFluidPacket)) {
super.moveItems(matrices, new SlotSingleItem(slot));
if (stack.getCount() > 1) {
this.stackSizeRenderer.renderStackSize(font, AEItemStack.fromItemStack(stack).getStackSize(), false, slot.xPos, slot.yPos);
}
return true;
if (slot instanceof FakeSlot && slot.getStack().getItem() instanceof ItemFluidPacket) {
FluidRenderUtils.renderFluidPacketIntoGuiSlot(slot, slot.getStack(), stackSizeRenderer, font);
return;
}
return false;
super.moveItems(matrices, slot);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import com.glodblock.github.loader.FCItems;
import com.glodblock.github.util.ConfigSet;
import com.glodblock.github.util.FCUtil;
import com.glodblock.github.util.HashUtil;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
Expand All @@ -46,7 +49,9 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ICraftingRecipe;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.IRecipeType;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.capability.CapabilityFluidHandler;
Expand All @@ -68,6 +73,12 @@ public class ContainerFluidPatternTerminal extends ItemTerminalContainer impleme
private final ICraftingHelper craftingHelper;
private final ConfigSet config = new ConfigSet();
private ICraftingRecipe currentRecipe;
// Fix lag from ae's dumb fuck code
private static final Cache<HashUtil.ItemHandlerHash, ICraftingRecipe> cache = CacheBuilder.newBuilder()
.initialCapacity(10)
.maximumSize(120)
.concurrencyLevel(10)
.build();
private boolean currentRecipeCraftingMode;
public static ContainerType<ContainerFluidPatternTerminal> TYPE = ContainerTypeBuilder
.create(ContainerFluidPatternTerminal::new, ITerminalHost.class)
Expand Down Expand Up @@ -135,23 +146,36 @@ public ContainerFluidPatternTerminal(int id, PlayerInventory ip, ITerminalHost m
@Override
public void putStackInSlot(int slotID, @Nonnull ItemStack stack) {
super.putStackInSlot(slotID, stack);
this.getAndUpdateOutput();
if (this.getSlot(slotID) instanceof FakeCraftingMatrixSlot) {
this.getAndUpdateOutput();
}
}

private ICraftingRecipe lookupRecipe(World world, CraftingInventory ic) {
HashUtil.ItemHandlerHash hash = HashUtil.hashItemHandler(this.craftingGridInv);
try {
ICraftingRecipe recipe = cache.get(hash, () -> world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, ic, world).orElse(NullRecipe.NULL));
return recipe == NullRecipe.NULL ? null : recipe;
} catch (Exception ignored) {
}
return null;
}

private ItemStack getAndUpdateOutput() {
World world = this.getPlayerInventory().player.world;
CraftingInventory ic = new CraftingInventory(this, 3, 3);
final World world = this.getPlayerInventory().player.world;
final CraftingInventory ic = new CraftingInventory(this, 3, 3);

for(int x = 0; x < ic.getSizeInventory(); ++x) {
for (int x = 0; x < ic.getSizeInventory(); x++) {
ic.setInventorySlotContents(x, this.craftingGridInv.getStackInSlot(x));
}

if (this.currentRecipe == null || !this.currentRecipe.matches(ic, world)) {
this.currentRecipe = world.getRecipeManager().getRecipe(IRecipeType.CRAFTING, ic, world).orElse(null);
this.currentRecipe = lookupRecipe(world, ic);
this.currentRecipeCraftingMode = this.craftingMode;
}

ItemStack is;
final ItemStack is;

if (this.currentRecipe == null) {
is = ItemStack.EMPTY;
} else {
Expand Down Expand Up @@ -457,9 +481,15 @@ public void detectAndSendChanges() {
if (this.isCraftingMode() != this.patternTerminal.isCraftingRecipe()) {
this.setCraftingMode(this.patternTerminal.isCraftingRecipe());
}
this.substitute = this.patternTerminal.isSubstitution();
this.combine = this.patternTerminal.getCombineMode();
this.fluidFirst = this.patternTerminal.getFluidPlaceMode();
if (this.substitute != this.patternTerminal.isSubstitution()) {
this.substitute = this.patternTerminal.isSubstitution();
}
if (this.combine != this.patternTerminal.getCombineMode()) {
this.combine = this.patternTerminal.getCombineMode();
}
if (this.fluidFirst != this.patternTerminal.getFluidPlaceMode()) {
this.fluidFirst = this.patternTerminal.getFluidPlaceMode();
}
}
}

Expand Down Expand Up @@ -555,4 +585,40 @@ public void set(String id, Object value) {
public Object get(String id) {
return this.config.getConfig(id);
}

@SuppressWarnings("all")
private static final class NullRecipe implements ICraftingRecipe {

private static final NullRecipe NULL = new NullRecipe();

@Override
public boolean matches(CraftingInventory inv, World worldIn) {
return false;
}

@Override
public ItemStack getCraftingResult(CraftingInventory inv) {
return null;
}

@Override
public boolean canFit(int width, int height) {
return false;
}

@Override
public ItemStack getRecipeOutput() {
return null;
}

@Override
public ResourceLocation getId() {
return null;
}

@Override
public IRecipeSerializer<?> getSerializer() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,16 @@ public void detectAndSendChanges() {
this.verifyPermissions(SecurityPermissions.BUILD, false);
super.detectAndSendChanges();
if (Platform.isServer()) {
fluidPacket = ((ExtendedInterface) dualityInterfaceCopy).getFluidPacketMode();
allowSplitting = ((ExtendedInterface) dualityInterfaceCopy).getSplittingMode();
blockModeEx = ((ExtendedInterface) dualityInterfaceCopy).getExtendedBlockMode();
ExtendedInterface eif = (ExtendedInterface) dualityInterfaceCopy;
if (fluidPacket != eif.getFluidPacketMode()) {
fluidPacket = eif.getFluidPacketMode();
}
if (allowSplitting != eif.getSplittingMode()) {
allowSplitting = eif.getSplittingMode();
}
if (blockModeEx != eif.getExtendedBlockMode()) {
blockModeEx = eif.getExtendedBlockMode();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.glodblock.github.handler;

import appeng.core.Api;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.*;
import com.glodblock.github.client.container.*;
import com.glodblock.github.client.model.FluidEncodedPatternModel;
import com.glodblock.github.client.model.FluidPacketModel;
import com.glodblock.github.common.part.PartDualInterface;
import com.glodblock.github.common.part.PartFluidPatternTerminal;
import com.glodblock.github.interfaces.HasCustomModel;
import com.glodblock.github.loader.FCBlocks;
import com.glodblock.github.util.Ae2ReflectClient;
import com.glodblock.github.util.FluidRenderUtils;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.RenderTypeLookup;
import net.minecraft.client.renderer.model.ModelResourceLocation;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.item.Item;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.event.TextureStitchEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.eventbus.api.SubscribeEvent;
Expand Down Expand Up @@ -50,10 +50,6 @@ public void onRegisterModels(ModelRegistryEvent event) {
for (Pair<String, Item> entry : items) {
registerModel(entry.getLeft(), entry.getRight());
}

Api.instance().registries().partModels().registerModels(PartDualInterface.MODELS);
Api.instance().registries().partModels().registerModels(PartFluidPatternTerminal.MODELS);
//AEApi.instance().registries().partModels().registerModels(PartExtendedFluidPatternTerminal.MODELS);
}

private static void registerModel(String key, Item item) {
Expand All @@ -63,4 +59,11 @@ private static void registerModel(String key, Item item) {
"inventory"));
}

@SubscribeEvent
public void onTextureMapStitch(TextureStitchEvent event) {
if (event.getMap().getTextureLocation().equals(PlayerContainer.LOCATION_BLOCKS_TEXTURE)) {
FluidRenderUtils.resetCache();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import appeng.block.AEBaseBlockItem;
import appeng.block.AEBaseTileBlock;
import appeng.core.Api;
import appeng.core.features.ActivityState;
import appeng.core.features.BlockStackSrc;
import appeng.tile.AEBaseTileEntity;
import com.glodblock.github.FluidCraft;
import com.glodblock.github.client.container.*;
import com.glodblock.github.common.part.PartDualInterface;
import com.glodblock.github.common.part.PartFluidPatternTerminal;
import com.glodblock.github.loader.FCItems;
import com.glodblock.github.util.FCUtil;
import net.minecraft.block.Block;
Expand Down Expand Up @@ -63,6 +66,7 @@ public void onRegisterItems(RegistryEvent.Register<Item> event) {
for (Pair<String, Item> entry : items) {
event.getRegistry().register(initItem(entry.getLeft(), entry.getRight()));
}
this.registerPartModel();
}

@SubscribeEvent
Expand Down Expand Up @@ -103,4 +107,9 @@ public void onInit() {
}
}

private void registerPartModel() {
Api.instance().registries().partModels().registerModels(PartDualInterface.MODELS);
Api.instance().registries().partModels().registerModels(PartFluidPatternTerminal.MODELS);
}

}
Loading

0 comments on commit 34be221

Please sign in to comment.