Merge branch 'HbmMods:master' into master

This commit is contained in:
Bufka2011 2026-01-11 12:24:43 -07:00 committed by GitHub
commit c146829bf4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 183 additions and 117 deletions

View File

@ -1,23 +1,29 @@
package com.hbm.inventory.gui; package com.hbm.inventory.gui;
import com.hbm.lib.RefStrings; import com.hbm.util.Tuple;
import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField; import net.minecraft.client.gui.GuiTextField;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Keyboard; import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.MathContext; import java.math.MathContext;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Locale; import java.util.Locale;
import java.util.Stack; import java.util.Stack;
public class GUICalculator extends GuiScreen { public class GUICalculator extends GuiScreen {
private static final ResourceLocation texture = new ResourceLocation(RefStrings.MODID, "textures/gui/calculator.png");
private int xSize = 220; private int xSize = 220;
private int ySize = 50; private int ySize = 50;
private int borderWidth = 2;
private GuiTextField inputField; private GuiTextField inputField;
private int selectedHist = -1;
private static final int maxHistory = 6;
private static Deque<Tuple.Pair<String, Double>> history = new ArrayDeque<>();
private String latestResult = "?"; private String latestResult = "?";
@Override @Override
@ -46,15 +52,31 @@ public class GUICalculator extends GuiScreen {
String input = inputField.getText().replaceAll("[^\\d+\\-*/^!.()\\sA-Za-z]+", ""); String input = inputField.getText().replaceAll("[^\\d+\\-*/^!.()\\sA-Za-z]+", "");
if (p_73869_1_ == 13 || p_73869_1_ == 10) { // when pressing enter (CR or LF) if (p_73869_1_ == 13 || p_73869_1_ == 10) { // when pressing enter (CR or LF)
try { if (selectedHist != -1) {
double result = evaluateExpression(input); input = new ArrayList<>(history).get(selectedHist).key;
String plainStringRepresentation = (new BigDecimal(result, MathContext.DECIMAL64)).toPlainString(); inputField.setText(input);
GuiScreen.setClipboardString(plainStringRepresentation); selectedHist = -1;
inputField.setText(plainStringRepresentation); } else {
inputField.setCursorPositionEnd(); try {
inputField.setSelectionPos(0); double result = evaluateExpression(input);
} catch (Exception ignored) {} history.addFirst(new Tuple.Pair<String,Double>(input, result));
return; if (history.size() > maxHistory) history.removeLast();
String plainStringRepresentation = (new BigDecimal(result, MathContext.DECIMAL64)).toPlainString();
GuiScreen.setClipboardString(plainStringRepresentation);
inputField.setText(plainStringRepresentation);
inputField.setCursorPositionEnd();
inputField.setSelectionPos(0);
} catch (Exception ignored) {}
return;
}
}
if (p_73869_2_ == 200) { // up arrow
selectedHist = Math.max(selectedHist - 1, -1);
} else if (p_73869_2_ == 208) { // down arrow
selectedHist = Math.min(selectedHist + 1, history.size() - 1);
} else {
selectedHist = -1;
} }
if (input.isEmpty()) { if (input.isEmpty()) {
@ -72,14 +94,27 @@ public class GUICalculator extends GuiScreen {
super.drawScreen(mouseX, mouseY, partialTicks); super.drawScreen(mouseX, mouseY, partialTicks);
GL11.glColor4f(1F, 1F, 1F, 1F); GL11.glColor4f(1F, 1F, 1F, 1F);
mc.getTextureManager().bindTexture(texture);
int x = (width - xSize) / 2; int x = (width - xSize) / 2;
int y = (height - ySize) / 2; int y = (height - ySize) / 2;
int histHeight = (fontRendererObj.FONT_HEIGHT + 2) * maxHistory;
int histStart = y + 30 + fontRendererObj.FONT_HEIGHT + 8;
drawTexturedModalRect(x, y, 0, 0, xSize, ySize); drawRect(x, y, x+xSize, y+ySize+histHeight, 0xFF2d2d2d);
drawRect(x+borderWidth, y+borderWidth, x+xSize-borderWidth, y+ySize-borderWidth+histHeight, 0xFF3d3d3d);
drawRect(x, histStart - 5, x+xSize, histStart - 3, 0xFF2d2d2d);
inputField.drawTextBox(); inputField.drawTextBox();
fontRendererObj.drawString("=" + latestResult, x + 5, y + 30, -1); fontRendererObj.drawString("=" + latestResult, x + 5, y + 30, -1);
int i = 0;
for (Tuple.Pair<String, Double> prevInput : history) {
int hy = y + 50 + (fontRendererObj.FONT_HEIGHT+1)*i;
if (i == selectedHist) {
drawRect(x + 4, hy - 1, x + 4 + xSize - 9, hy + fontRendererObj.FONT_HEIGHT, 0xFF111111);
}
fontRendererObj.drawString(prevInput.key + " = " + prevInput.value, x + 5, hy, -1);
i++;
}
} }
/** /**

View File

@ -2,6 +2,7 @@ package com.hbm.uninos.networkproviders;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -67,109 +68,139 @@ public class PneumaticNetwork extends NodeNet {
if(sendOrder == SEND_LAST) BobMathUtil.reverseIntArray(sourceSlotAccess); if(sendOrder == SEND_LAST) BobMathUtil.reverseIntArray(sourceSlotAccess);
if(sendOrder == SEND_RANDOM) BobMathUtil.shuffleIntArray(sourceSlotAccess); if(sendOrder == SEND_RANDOM) BobMathUtil.shuffleIntArray(sourceSlotAccess);
ISidedInventory sidedSource = source instanceof ISidedInventory ? (ISidedInventory) source : null;
boolean hasItem = false;
for(int i : sourceSlotAccess) {
ItemStack stack = source.getStackInSlot(i);
if(stack != null) {
if(sidedSource != null && !sidedSource.canExtractItem(i, stack, sourceSide)) continue;
boolean match = tube.matchesFilter(stack);
if((match && !tube.whitelist) || (!match && tube.whitelist)) continue;
hasItem = true;
break;
}
}
// return early if there arent any items in the source inventory, saves on some cpu usage for idle networks
if(!hasItem) return false;
// for round robin, receivers are ordered by proximity to the source // for round robin, receivers are ordered by proximity to the source
ReceiverComparator comparator = new ReceiverComparator(tube); ReceiverComparator comparator = new ReceiverComparator(tube);
List<Entry<IInventory, Triplet<ForgeDirection, Long, TileEntityPneumoTube>>> receiverList = new ArrayList(receivers.size()); List<Entry<IInventory, Triplet<ForgeDirection, Long, TileEntityPneumoTube>>> receiverList = new ArrayList(receivers.size());
receiverList.addAll(receivers.entrySet()); receiverList.addAll(receivers.entrySet());
receiverList.sort(comparator);
int index = nextReceiver % receivers.size(); if(receiveOrder == RECEIVE_ROBIN) receiverList.sort(comparator);
Entry<IInventory, Triplet<ForgeDirection, Long, TileEntityPneumoTube>> chosenReceiverEntry = null; if(receiveOrder == RECEIVE_RANDOM) Collections.shuffle(receiverList);
if(receiveOrder == RECEIVE_ROBIN) chosenReceiverEntry = receiverList.get(index);
if(receiveOrder == RECEIVE_RANDOM) chosenReceiverEntry = receiverList.get(rand.nextInt(receiverList.size()));
if(chosenReceiverEntry == null) return false;
IInventory dest = chosenReceiverEntry.getKey();
TileEntityPneumoTube endpointTile = chosenReceiverEntry.getValue().getZ();
ISidedInventory sidedDest = dest instanceof ISidedInventory ? (ISidedInventory) dest : null;
ISidedInventory sidedSource = source instanceof ISidedInventory ? (ISidedInventory) source : null;
TileEntity tile1 = source instanceof TileEntity ? (TileEntity) source : null; TileEntity tile1 = source instanceof TileEntity ? (TileEntity) source : null;
TileEntity tile2 = dest instanceof TileEntity ? (TileEntity) dest : null;
// range check for our compression level, skip if either source or dest aren't tile entities int attempts = 0;
if(tile1 != null && tile2 != null) { int maxAttempts = receiverList.size();
int sq = (tile1.xCoord - tile2.xCoord) * (tile1.xCoord - tile2.xCoord) + (tile1.yCoord - tile2.yCoord) * (tile1.yCoord - tile2.yCoord) + (tile1.zCoord - tile2.zCoord) * (tile1.zCoord - tile2.zCoord);
if(sq > maxRange * maxRange) { // try all receivers for both modes, in an attempts based system.
return false; // instead of bailing out of trying after the first failure (which means you have to wait 0.25 seconds), we just try the next one.
while(attempts < maxAttempts) {
int index = (receiveOrder == RECEIVE_ROBIN) ? (nextReceiver + attempts) % receiverList.size() : attempts;
Entry<IInventory, Triplet<ForgeDirection, Long, TileEntityPneumoTube>> candidate = receiverList.get(index);
if(NodeNet.isBadLink(candidate.getKey())) {
receivers.remove(candidate.getKey());
attempts++;
continue;
} }
}
int destSide = chosenReceiverEntry.getValue().getX().getOpposite().ordinal(); IInventory dest = candidate.getKey();
int[] destSlotAccess = getSlotAccess(dest, destSide); TileEntityPneumoTube endpointTile = candidate.getValue().getZ();
int itemsLeftToSend = ITEMS_PER_TRANSFER; // not actually individual items, but rather the total "mass", based on max stack size TileEntity tile2 = dest instanceof TileEntity ? (TileEntity) dest : null;
int itemHardCap = dest instanceof TileEntityMachineAutocrafter ? 1 : ITEMS_PER_TRANSFER;
boolean didSomething = false;
for(int sourceIndex : sourceSlotAccess) { // range check for our compression level, skip if either source or dest aren't tile entities
ItemStack sourceStack = source.getStackInSlot(sourceIndex); if(tile1 != null && tile2 != null) {
if(sourceStack == null) continue; int sq = (tile1.xCoord - tile2.xCoord) * (tile1.xCoord - tile2.xCoord) + (tile1.yCoord - tile2.yCoord) * (tile1.yCoord - tile2.yCoord) + (tile1.zCoord - tile2.zCoord) * (tile1.zCoord - tile2.zCoord);
if(sidedSource != null && !sidedSource.canExtractItem(sourceIndex, sourceStack, sourceSide)) continue; if(sq > maxRange * maxRange) {
// filter of the source attempts++;
boolean match = tube.matchesFilter(sourceStack); continue;
if((match && !tube.whitelist) || (!match && tube.whitelist)) continue; }
// filter of the receiver, only if the sender and receiver aren't the same block
if(endpointTile != null && endpointTile != tube) {
match = endpointTile.matchesFilter(sourceStack);
if((match && !endpointTile.whitelist) || (!match && endpointTile.whitelist)) continue;
} }
// the "mass" of an item. something that only stacks to 4 has a "mass" of 16. max transfer mass is 64, i.e. one standard stack, or one single unstackable item
int proportionalValue = MathHelper.clamp_int(64 / sourceStack.getMaxStackSize(), 1, 64);
// try to fill partial stacks first ISidedInventory sidedDest = dest instanceof ISidedInventory ? (ISidedInventory) dest : null;
for(int destIndex : destSlotAccess) { int destSide = candidate.getValue().getX().getOpposite().ordinal();
ItemStack destStack = dest.getStackInSlot(destIndex); int[] destSlotAccess = getSlotAccess(dest, destSide);
if(destStack == null) continue; int itemsLeftToSend = ITEMS_PER_TRANSFER; // not actually individual items, but rather the total "mass", based on max stack size
if(!ItemStackUtil.areStacksCompatible(sourceStack, destStack)) continue; int itemHardCap = dest instanceof TileEntityMachineAutocrafter ? 1 : ITEMS_PER_TRANSFER;
int toMove = BobMathUtil.min(sourceStack.stackSize, destStack.getMaxStackSize() - destStack.stackSize, dest.getInventoryStackLimit() - destStack.stackSize, itemsLeftToSend / proportionalValue, itemHardCap); boolean didSomething = false;
if(toMove <= 0) continue;
ItemStack checkStack = destStack.copy(); for(int sourceIndex : sourceSlotAccess) {
checkStack.stackSize += toMove; ItemStack sourceStack = source.getStackInSlot(sourceIndex);
if(!dest.isItemValidForSlot(destIndex, checkStack)) continue; if(sourceStack == null) continue;
if(sidedDest != null && !sidedDest.canInsertItem(destIndex, checkStack, destSide)) continue; if(sidedSource != null && !sidedSource.canExtractItem(sourceIndex, sourceStack, sourceSide)) continue;
// filter of the source
boolean match = tube.matchesFilter(sourceStack);
if((match && !tube.whitelist) || (!match && tube.whitelist)) continue;
// filter of the receiver, only if the sender and receiver aren't the same block
if(endpointTile != null && endpointTile != tube) {
match = endpointTile.matchesFilter(sourceStack);
if((match && !endpointTile.whitelist) || (!match && endpointTile.whitelist)) continue;
}
// the "mass" of an item. something that only stacks to 4 has a "mass" of 16. max transfer mass is 64, i.e. one standard stack, or one single unstackable item
int proportionalValue = MathHelper.clamp_int(64 / sourceStack.getMaxStackSize(), 1, 64);
// try to fill partial stacks first
for(int destIndex : destSlotAccess) {
ItemStack destStack = dest.getStackInSlot(destIndex);
if(destStack == null) continue;
if(!ItemStackUtil.areStacksCompatible(sourceStack, destStack)) continue;
int toMove = BobMathUtil.min(sourceStack.stackSize, destStack.getMaxStackSize() - destStack.stackSize, dest.getInventoryStackLimit() - destStack.stackSize, itemsLeftToSend / proportionalValue, itemHardCap);
if(toMove <= 0) continue;
ItemStack checkStack = destStack.copy();
checkStack.stackSize += toMove;
if(!dest.isItemValidForSlot(destIndex, checkStack)) continue;
if(sidedDest != null && !sidedDest.canInsertItem(destIndex, checkStack, destSide)) continue;
sourceStack.stackSize -= toMove;
if(sourceStack.stackSize <= 0) source.setInventorySlotContents(sourceIndex, null);
destStack.stackSize += toMove;
itemsLeftToSend -= toMove * proportionalValue;
didSomething = true;
if(itemsLeftToSend <= 0) break;
}
// if there's stuff left to send, occupy empty slots
if(itemsLeftToSend > 0 && sourceStack.stackSize > 0) for(int destIndex : destSlotAccess) {
if(dest.getStackInSlot(destIndex) != null) continue;
int toMove = BobMathUtil.min(sourceStack.stackSize, dest.getInventoryStackLimit(), itemsLeftToSend / proportionalValue, itemHardCap);
if(toMove <= 0) continue;
ItemStack checkStack = sourceStack.copy();
checkStack.stackSize = toMove;
if(!dest.isItemValidForSlot(destIndex, checkStack)) continue;
if(sidedDest != null && !sidedDest.canInsertItem(destIndex, checkStack, destSide)) continue;
ItemStack newStack = sourceStack.copy();
newStack.stackSize = toMove;
sourceStack.stackSize -= toMove;
if(sourceStack.stackSize <= 0) source.setInventorySlotContents(sourceIndex, null);
dest.setInventorySlotContents(destIndex, newStack);
itemsLeftToSend -= toMove * proportionalValue;
didSomething = true;
if(itemsLeftToSend <= 0) break;
}
sourceStack.stackSize -= toMove;
if(sourceStack.stackSize <= 0) source.setInventorySlotContents(sourceIndex, null);
destStack.stackSize += toMove;
itemsLeftToSend -= toMove * proportionalValue;
didSomething = true;
if(itemsLeftToSend <= 0) break; if(itemsLeftToSend <= 0) break;
} }
// if there's stuff left to send, occupy empty slots // make sure both parties are saved to disk and increment the counter for round robin
if(itemsLeftToSend > 0 && sourceStack.stackSize > 0) for(int destIndex : destSlotAccess) { if(didSomething) {
if(dest.getStackInSlot(destIndex) != null) continue; source.markDirty();
int toMove = BobMathUtil.min(sourceStack.stackSize, dest.getInventoryStackLimit(), itemsLeftToSend / proportionalValue, itemHardCap); dest.markDirty();
if(toMove <= 0) continue; return true;
ItemStack checkStack = sourceStack.copy();
checkStack.stackSize = toMove;
if(!dest.isItemValidForSlot(destIndex, checkStack)) continue;
if(sidedDest != null && !sidedDest.canInsertItem(destIndex, checkStack, destSide)) continue;
ItemStack newStack = sourceStack.copy();
newStack.stackSize = toMove;
sourceStack.stackSize -= toMove;
if(sourceStack.stackSize <= 0) source.setInventorySlotContents(sourceIndex, null);
dest.setInventorySlotContents(destIndex, newStack);
itemsLeftToSend -= toMove * proportionalValue;
didSomething = true;
if(itemsLeftToSend <= 0) break;
} }
if(itemsLeftToSend <= 0) break; attempts++;
} }
return false;
// make sure both parties are saved to disk and increment the counter for round robin
if(didSomething) {
source.markDirty();
dest.markDirty();
}
return didSomething;
} }
/** Returns an array of accessible slots from the given side of an IInventory. If it's an ISidedInventory, uses the sided restrictions instead. */ /** Returns an array of accessible slots from the given side of an IInventory. If it's an ISidedInventory, uses the sided restrictions instead. */

Binary file not shown.

Before

Width:  |  Height:  |  Size: 566 B