Skip to content

Commit

Permalink
merge trunk/1.18.2 including the PRs released in 1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lothrazar committed Jun 12, 2023
2 parents 81cab78 + ce584d7 commit 2959828
Show file tree
Hide file tree
Showing 22 changed files with 461 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ crash-reports/
*.lnk
*.jks
secret.properties
.vscode
6 changes: 4 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ org.gradle.daemon=false
# as needed run/server.properties : online-mode=false
# implementation fg.deobf("curse.maven:simple-storage-network-268495:3163007")
curse_id=268495
mod_version=1.6.5

mod_version=1.7.0


# optional dependencies
Expand All @@ -20,8 +21,9 @@ curios_version=5.1.1.0
patchouli_version=77



# folder to copy the build output jar
dist_folder=c:/temp
dist_folder=

# for eclipse.ini if needed
#-vm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.lothrazar.storagenetwork.api;

import java.util.List;

import com.lothrazar.storagenetwork.util.StackProviderBatch;

import net.minecraft.world.item.ItemStack;

/**
Expand Down Expand Up @@ -80,4 +83,8 @@ public interface IConnectableLink {
void setPriority(int value);

void setFilter(int value, ItemStack copy);

public ItemStack extractFromSlot(int slot, int amount, boolean simulate);

void addToStackProviderBatch(StackProviderBatch availableItems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface IItemStackMatcher {
* @return
*/
boolean match(ItemStack stack);

boolean match(IItemStackMatcher matcher);
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public boolean keyPressed(int keyCode, int scanCode, int b) {
}
return true;
}
else if (network.stackUnderMouse.isEmpty()) {
else if (!network.stackUnderMouse.isEmpty()) {
try {
JeiHooks.testJeiKeybind(mouseKey, network.stackUnderMouse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import com.lothrazar.storagenetwork.api.IItemStackMatcher;
import com.lothrazar.storagenetwork.capability.handler.ItemStackMatcher;
import com.lothrazar.storagenetwork.registry.StorageNetworkCapabilities;
import com.lothrazar.storagenetwork.util.RequestBatch;
import com.lothrazar.storagenetwork.util.StackProvider;
import com.lothrazar.storagenetwork.util.StackProviderBatch;
import com.lothrazar.storagenetwork.util.UtilInventory;
import com.lothrazar.storagenetwork.util.UtilTileEntity;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand Down Expand Up @@ -302,6 +306,21 @@ public ItemStack request(ItemStackMatcher matcher, int size, boolean simulate) {
return ItemHandlerHelper.copyStackWithSize(usedMatcher.getStack(), alreadyTransferred);
}

public void executeRequestBatch(RequestBatch batch) {
StackProviderBatch availableItems = new StackProviderBatch();
for (IConnectableLink storage : getSortedConnectableStorage()) {
storage.addToStackProviderBatch(availableItems);
}
for (Item item : batch.keySet()) {
List<StackProvider> availableStacks = availableItems.get(item);
if (availableStacks != null) {
for (StackProvider provider : availableStacks) {
batch.extractStacks(provider.getStorage(), provider.getSlot(), item);
}
}
}
}

/**
* TODO: move or refactor?
*/
Expand Down
74 changes: 34 additions & 40 deletions src/main/java/com/lothrazar/storagenetwork/block/main/TileMain.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lothrazar.storagenetwork.block.main;

import java.util.Set;

import com.lothrazar.storagenetwork.StorageNetworkMod;
import com.lothrazar.storagenetwork.api.DimPos;
import com.lothrazar.storagenetwork.api.EnumStorageDirection;
Expand All @@ -10,6 +11,8 @@
import com.lothrazar.storagenetwork.capability.handler.ItemStackMatcher;
import com.lothrazar.storagenetwork.registry.SsnRegistry;
import com.lothrazar.storagenetwork.registry.StorageNetworkCapabilities;
import com.lothrazar.storagenetwork.util.Request;
import com.lothrazar.storagenetwork.util.RequestBatch;
import com.lothrazar.storagenetwork.util.UtilInventory;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -68,6 +71,11 @@ public ItemStack request(ItemStackMatcher matcher, int size, boolean simulate) {
return result;
}

public void executeRequestBatch(RequestBatch batch) {
batch.sort();
nw.executeRequestBatch(batch);
}

private DimPos getDimPos() {
return new DimPos(level, worldPosition);
}
Expand Down Expand Up @@ -178,12 +186,15 @@ private void updateProcess() {
*/
private void updateExports() {
Set<IConnectable> conSet = nw.getConnectables();
RequestBatch requestBatch = new RequestBatch();

for (IConnectable connectable : conSet) {
if (connectable == null || connectable.getPos() == null) {
// StorageNetwork.log("null connectable or pos : updateExports() ");
// StorageNetwork.log("null connectable or pos : updateExports() ");
continue;
}
IConnectableItemAutoIO storage = connectable.getPos().getCapability(StorageNetworkCapabilities.CONNECTABLE_AUTO_IO, null);
IConnectableItemAutoIO storage = connectable.getPos()
.getCapability(StorageNetworkCapabilities.CONNECTABLE_AUTO_IO, null);
if (storage == null) {
continue;
}
Expand All @@ -199,66 +210,49 @@ private void updateExports() {
if (storage.needsRedstone()) {
boolean power = level.hasNeighborSignal(connectable.getPos().getBlockPos());
if (power == false) {
// StorageNetwork.log(power + " Export pow here ; needs yes skip me");
// StorageNetwork.log(power + " Export pow here ; needs yes skip me");
continue;
}
}
for (IItemStackMatcher matcher : storage.getAutoExportList()) {
if (matcher.getStack().isEmpty()) {
continue;
}
//default amt to request. can be overriden by other upgrades
int amtToRequest = storage.getTransferRate();
//check operations upgrade for export

Request request = new Request(storage);
// default amt to request. can be overriden by other upgrades
// check operations upgrade for export
boolean stockMode = storage.isStockMode();
if (stockMode) {
StorageNetworkMod.log("stockMode == TRUE ; updateExports: attempt " + matcher.getStack());
//STOCK upgrade means
// STOCK upgrade means
try {
BlockEntity tileEntity = level.getBlockEntity(connectable.getPos().getBlockPos().relative(storage.facingInventory()));
IItemHandler targetInventory = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).orElse(null);
//request with false to see how many even exist in there.
int stillNeeds = UtilInventory.containsAtLeastHowManyNeeded(targetInventory, matcher.getStack(), matcher.getStack().getCount());
BlockEntity tileEntity = level
.getBlockEntity(connectable.getPos().getBlockPos().relative(storage.facingInventory()));
IItemHandler targetInventory = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)
.orElse(null);
// request with false to see how many even exist in there.
int stillNeeds = UtilInventory.containsAtLeastHowManyNeeded(targetInventory, matcher.getStack(),
matcher.getStack().getCount());
if (stillNeeds == 0) {
//they dont need any more, they have the stock they need
// they dont need any more, they have the stock they need
StorageNetworkMod.log("stockMode continnue; canc");
continue;
}
amtToRequest = Math.min(stillNeeds, amtToRequest);
StorageNetworkMod.log("updateExports stock mode edited value: amtToRequest = " + amtToRequest);
}
catch (Throwable e) {
request.setCount(Math.min(stillNeeds, request.getCount()));
StorageNetworkMod.log("updateExports stock mode edited value: amtToRequest = " + request.getCount());
} catch (Throwable e) {
StorageNetworkMod.LOGGER.error("Error thrown from a connected block" + e);
}
}
if (matcher.getStack().isEmpty() || amtToRequest == 0) {
//either the thing is empty or we are requesting none
continue;
}
ItemStack requestedStack = this.request((ItemStackMatcher) matcher, amtToRequest, true);
if (requestedStack.isEmpty()) {
continue;
}
// StorageNetwork.log("updateExports: found requestedStack = " + requestedStack);
// The stack is available in the network, let's simulate inserting it into the storage
ItemStack insertedSim = storage.insertStack(requestedStack, true);
// Determine the amount of items moved in the stack
if (!insertedSim.isEmpty()) {
int movedItems = requestedStack.getCount() - insertedSim.getCount();
if (movedItems <= 0) {
continue;
}
requestedStack.setCount(movedItems);
}
// Alright, some items got moved in the simulation. Let's do it for real this time.
ItemStack realExtractedStack = request(new ItemStackMatcher(requestedStack, false, true), requestedStack.getCount(), false);
if (realExtractedStack.isEmpty()) {
if (matcher.getStack().isEmpty() || request.getCount() == 0) {
// either the thing is empty or we are requesting none
continue;
}
storage.insertStack(realExtractedStack, false);
break;
requestBatch.put(matcher.getStack().getItem(), request);
}
}
executeRequestBatch(requestBatch);
}

public static void clientTick(Level level, BlockPos blockPos, BlockState blockState, TileMain tile) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public boolean keyPressed(int keyCode, int scanCode, int b) {
}
return true;
}
else if (network.stackUnderMouse.isEmpty()) {
else if (!network.stackUnderMouse.isEmpty()) {
try {
JeiHooks.testJeiKeybind(mouseKey, network.stackUnderMouse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,9 @@ public Direction facingInventory() {
public UpgradesItemStackHandler getUpgrades() {
return upgrades;
}

public void extractFromSlot(int slot){

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.lothrazar.storagenetwork.StorageNetworkMod;
import com.lothrazar.storagenetwork.api.DimPos;
import com.lothrazar.storagenetwork.api.EnumStorageDirection;
Expand All @@ -11,6 +12,9 @@
import com.lothrazar.storagenetwork.api.IItemStackMatcher;
import com.lothrazar.storagenetwork.capability.handler.FilterItemStackHandler;
import com.lothrazar.storagenetwork.registry.StorageNetworkCapabilities;
import com.lothrazar.storagenetwork.util.StackProvider;
import com.lothrazar.storagenetwork.util.StackProviderBatch;

import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
Expand Down Expand Up @@ -264,4 +268,52 @@ public void deserializeNBT(CompoundTag nbt) {
}
}
}

public ItemStack extractFromSlot(int slot, int amount, boolean simulate) {
DimPos inventoryPos = connectable.getPos().offset(inventoryFace);
// Test whether the connected block has the IItemHandler capability
IItemHandler itemHandler = inventoryPos.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
inventoryFace.getOpposite());
if (itemHandler == null) {
return ItemStack.EMPTY;
}
return itemHandler.extractItem(slot, amount, simulate);
}

public void addToStackProviderBatch(StackProviderBatch availableItems) {
// If this storage is configured to only export from the network, do not
// extract from the storage, but abort immediately.
if (filterDirection == EnumStorageDirection.IN) {
return;
}
if (inventoryFace == null) {
return;
}
DimPos inventoryPos = connectable.getPos().offset(inventoryFace);
// Test whether the connected block has the IItemHandler capability
IItemHandler itemHandler = inventoryPos.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
inventoryFace.getOpposite());
if (itemHandler == null) {
return;
}
// if (itemHandler instanceof ExchangeItemStackHandler) {
// StorageNetwork.log("cannot loop back a network extract into
// ExchangeItemStackHandler");
// return ItemStack.EMPTY;
// }
for (int slot = 0; slot < itemHandler.getSlots(); slot++) {
// force simulate: allow them to not let me see the stack, also dont extract
// since it might steal/dupe
ItemStack stack = itemHandler.extractItem(slot, 1, true);
if (stack == null || stack.isEmpty()) {
continue;
}
// Ignore stacks that are filtered
if (filters.isStackFiltered(stack)) {
continue;
}
StackProvider provider = new StackProvider(this, slot);
availableItems.put(stack.getItem(), provider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public ItemStackMatcher(ItemStack stack, boolean ore, boolean nbt) {
this.nbt = nbt;
}

private ItemStackMatcher() {}
private ItemStackMatcher() {
}

public void readFromNBT(CompoundTag compound) {
CompoundTag c = (CompoundTag) compound.get("stack");
Expand Down Expand Up @@ -84,4 +85,9 @@ public boolean match(ItemStack stackIn) {
}
return stackIn.getItem() == stack.getItem();
}

public boolean match(IItemStackMatcher matcher) {
ItemStack stack = matcher.getStack();
return match(stack);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public boolean keyPressed(int keyCode, int scanCode, int b) {
network.searchBar.keyPressed(keyCode, scanCode, b);
return true;
}
else if (network.stackUnderMouse.isEmpty()) {
else if (!network.stackUnderMouse.isEmpty()) {
try {
JeiHooks.testJeiKeybind(mouseKey, network.stackUnderMouse);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public boolean keyPressed(int keyCode, int scanCode, int b) {
network.searchBar.keyPressed(keyCode, scanCode, b);
return true;
}
else if (network.stackUnderMouse.isEmpty()) {
else if (!network.stackUnderMouse.isEmpty()) {
try {
JeiHooks.testJeiKeybind(mouseKey, network.stackUnderMouse);
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/lothrazar/storagenetwork/util/Batch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lothrazar.storagenetwork.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.minecraft.world.item.Item;

public class Batch<K> extends HashMap<Item, List<K>> {
public List<K> put(Item item, K object) {
if (containsKey(item)) {
List<K> matchingList = super.get(item);
matchingList.add(object);
return matchingList;
}
List<K> newList = new ArrayList<K>();
newList.add(object);
return super.put(item, newList);
}
}
Loading

0 comments on commit 2959828

Please sign in to comment.