probably increase performance by a lot

This commit is contained in:
Ell 2020-11-28 02:15:29 +01:00
parent 37f0e57868
commit 782ab181ce
6 changed files with 35 additions and 25 deletions

View file

@ -1,7 +1,5 @@
package de.ellpeck.prettypipes.network;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import de.ellpeck.prettypipes.misc.ItemEqualityType;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import net.minecraft.item.ItemStack;
@ -12,9 +10,11 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.INBTSerializable;
import net.minecraftforge.items.IItemHandler;
import org.apache.commons.lang3.tuple.Pair;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class NetworkLocation implements INBTSerializable<CompoundNBT> {
@ -37,17 +37,16 @@ public class NetworkLocation implements INBTSerializable<CompoundNBT> {
if (this.isEmpty(world))
return Collections.emptyList();
return this.getItems(world).entrySet().stream()
.filter(e -> ItemEqualityType.compareItems(e.getValue(), stack, equalityTypes))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
.filter(kv -> ItemEqualityType.compareItems(kv.getValue(), stack, equalityTypes) && this.canExtract(world, kv.getKey()))
.map(Map.Entry::getKey).collect(Collectors.toList());
}
public int getItemAmount(World world, ItemStack stack, ItemEqualityType... equalityTypes) {
if (this.isEmpty(world))
return 0;
return this.getItems(world).values().stream()
.filter(i -> ItemEqualityType.compareItems(stack, i, equalityTypes))
.mapToInt(ItemStack::getCount).sum();
return this.getItems(world).entrySet().stream()
.filter(kv -> ItemEqualityType.compareItems(stack, kv.getValue(), equalityTypes) && this.canExtract(world, kv.getKey()))
.mapToInt(kv -> kv.getValue().getCount()).sum();
}
public Map<Integer, ItemStack> getItems(World world) {
@ -55,19 +54,25 @@ public class NetworkLocation implements INBTSerializable<CompoundNBT> {
IItemHandler handler = this.getItemHandler(world);
if (handler != null) {
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i);
// check if the slot is accessible to us
if (handler.extractItem(i, 1, true).isEmpty())
if (stack.isEmpty())
continue;
if (this.itemCache == null)
this.itemCache = new HashMap<>();
// use getStackInSlot since there might be more than 64 items in there
this.itemCache.put(i, handler.getStackInSlot(i));
this.itemCache.put(i, stack);
}
}
}
return this.itemCache;
}
public boolean canExtract(World world, int slot) {
IItemHandler handler = this.getItemHandler(world);
return handler != null && !handler.extractItem(slot, 1, true).isEmpty();
}
public IItemHandler getItemHandler(World world) {
if (this.handlerCache == null) {
PipeNetwork network = PipeNetwork.get(world);

View file

@ -36,6 +36,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.common.util.Lazy;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
@ -76,6 +77,7 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
private int lastItemAmount;
private int priority;
private final LazyOptional<PipeTileEntity> lazyThis = LazyOptional.of(() -> this);
private final Lazy<Integer> workRandomizer = Lazy.of(() -> this.world.rand.nextInt(200));
public PipeTileEntity() {
this(Registry.pipeTileEntity);
@ -384,6 +386,10 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
this.cover = null;
}
public boolean shouldWorkNow(int speed) {
return (this.world.getGameTime() + this.workRandomizer.get()) % speed == 0;
}
@Override
public void remove() {
super.remove();

View file

@ -66,7 +66,7 @@ public class CraftingModuleItem extends ModuleItem {
@Override
public void tick(ItemStack module, PipeTileEntity tile) {
if (tile.getWorld().getGameTime() % this.speed != 0 || !tile.canWork())
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return;
PipeNetwork network = PipeNetwork.get(tile.getWorld());
// process crafting ingredient requests

View file

@ -1,12 +1,12 @@
package de.ellpeck.prettypipes.pipe.modules.extraction;
import de.ellpeck.prettypipes.Registry;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.items.IModule;
import de.ellpeck.prettypipes.items.ModuleItem;
import de.ellpeck.prettypipes.items.ModuleTier;
import de.ellpeck.prettypipes.misc.ItemFilter;
import de.ellpeck.prettypipes.network.PipeNetwork;
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
@ -31,9 +31,7 @@ public class ExtractionModuleItem extends ModuleItem {
@Override
public void tick(ItemStack module, PipeTileEntity tile) {
if (tile.getWorld().getGameTime() % this.speed != 0)
return;
if (!tile.canWork())
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return;
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);

View file

@ -31,9 +31,7 @@ public class RetrievalModuleItem extends ModuleItem {
@Override
public void tick(ItemStack module, PipeTileEntity tile) {
if (tile.getWorld().getGameTime() % this.speed != 0)
return;
if (!tile.canWork())
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
return;
PipeNetwork network = PipeNetwork.get(tile.getWorld());

View file

@ -176,10 +176,13 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
PipeTileEntity pipe = this.getConnectedPipe();
Map<EquatableItemStack, NetworkItem> items = new HashMap<>();
for (NetworkLocation location : network.getOrderedNetworkItems(pipe.getPos())) {
for (ItemStack stack : location.getItems(this.world).values()) {
EquatableItemStack equatable = new EquatableItemStack(stack, equalityTypes);
for (Map.Entry<Integer, ItemStack> entry : location.getItems(this.world).entrySet()) {
// make sure we can extract from this slot to display it
if (!location.canExtract(this.world, entry.getKey()))
continue;
EquatableItemStack equatable = new EquatableItemStack(entry.getValue(), equalityTypes);
NetworkItem item = items.computeIfAbsent(equatable, NetworkItem::new);
item.add(location, stack);
item.add(location, entry.getValue());
}
}
network.endProfile();