Skip to content

Commit

Permalink
Optimize Alf Portal logic (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdielKavash authored Mar 24, 2024
1 parent 052041f commit 89237f6
Showing 1 changed file with 74 additions and 7 deletions.
81 changes: 74 additions & 7 deletions src/main/java/vazkii/botania/common/block/tile/TileAlfPortal.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
*/
package vazkii.botania.common.block.tile;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

import net.minecraft.block.Block;
Expand Down Expand Up @@ -63,11 +65,14 @@ public class TileAlfPortal extends TileMod {

private static final String TAG_TICKS_OPEN = "ticksOpen";
private static final String TAG_TICKS_SINCE_LAST_ITEM = "ticksSinceLastItem";
private static final String TAG_STACK_COUNT = "stackCount";
private static final String TAG_STACK_COUNT = "stackCount"; // Input stacks
private static final String TAG_OUTPUT_COUNT = "outputStackCount";
private static final String TAG_STACK = "portalStack";
private static final String TAG_OUTPUT_STACK = "outputStack";
private static final String TAG_PORTAL_FLAG = "_elvenPortal";

List<ItemStack> stacksIn = new ArrayList();
List<ItemStack> stacksIn = new ArrayList<>();
Deque<ItemStack> stacksOut = new ArrayDeque<>();

public int ticksOpen = 0;
int ticksSinceLastItem = 0;
Expand Down Expand Up @@ -153,8 +158,10 @@ public void updateEntity() {
}

if(ticksSinceLastItem >= 4) {
if(!worldObj.isRemote)
if(!worldObj.isRemote) {
resolveRecipes();
spawnOutput();
}
}
}
} else closeNow = false;
Expand Down Expand Up @@ -244,12 +251,16 @@ void addItem(ItemStack stack) {
}

void resolveRecipes() {
if (stacksIn.isEmpty()) {
return;
}

int i = 0;
for(ItemStack stack : stacksIn) {
if(stack != null && stack.getItem() instanceof ILexicon) {
((ILexicon) stack.getItem()).unlockKnowledge(stack, BotaniaAPI.elvenKnowledge);
ItemLexicon.setForcedPage(stack, LexiconData.elvenMessage.getUnlocalizedName());
spawnItem(stack);
addOutput(stack);
stacksIn.remove(i);
return;
}
Expand All @@ -258,9 +269,48 @@ void resolveRecipes() {

for(RecipeElvenTrade recipe : BotaniaAPI.elvenTradeRecipes) {
if(recipe.matches(stacksIn, false)) {
recipe.matches(stacksIn, true);
spawnItem(recipe.getOutput().copy());
break;
ItemStack output = recipe.getOutput().copy();
int stackSize = output.stackSize;
output.stackSize = 0;
while (recipe.matches(stacksIn, true)) {
output.stackSize += stackSize;
}
addOutput(output);
if (stacksIn.isEmpty()) break;
}
}
}

void addOutput(ItemStack stack) {
if (stack == null) {
return;
}
if (stacksOut.isEmpty()) {
stacksOut.addLast(stack);
return;
}

ItemStack lastStack = stacksOut.getLast();
if (stack.isItemEqual(lastStack)) {
lastStack.stackSize += stack.stackSize;
} else {
stacksOut.addLast(stack);
}
}

void spawnOutput() {
if (!stacksOut.isEmpty()) {
ItemStack stack = stacksOut.getFirst();
if (stack.stackSize > 1) {
--stack.stackSize;
stack = stack.copy();
stack.stackSize = 1;
spawnItem(stack);
} else if (stack.stackSize == 1) {
stacksOut.removeFirst();
spawnItem(stack);
} else {
stacksOut.removeFirst();
}
}
}
Expand All @@ -284,6 +334,15 @@ public void writeToNBT(NBTTagCompound cmp) {
cmp.setTag(TAG_STACK + i, stackcmp);
i++;
}

cmp.setInteger(TAG_OUTPUT_COUNT, stacksOut.size());
i = 0;
for(ItemStack stack : stacksOut) {
NBTTagCompound stackcmp = new NBTTagCompound();
stack.writeToNBT(stackcmp);
cmp.setTag(TAG_OUTPUT_STACK + i, stackcmp);
i++;
}
}

@Override
Expand All @@ -297,6 +356,14 @@ public void readFromNBT(NBTTagCompound cmp) {
ItemStack stack = ItemStack.loadItemStackFromNBT(stackcmp);
stacksIn.add(stack);
}

count = cmp.getInteger(TAG_OUTPUT_COUNT);
stacksOut.clear();
for(int i = 0; i < count; i++) {
NBTTagCompound stackcmp = cmp.getCompoundTag(TAG_OUTPUT_STACK + i);
ItemStack stack = ItemStack.loadItemStackFromNBT(stackcmp);
stacksOut.addLast(stack);
}
}

@Override
Expand Down

0 comments on commit 89237f6

Please sign in to comment.