mirror of
https://github.com/Ellpeck/PrettyPipes.git
synced 2024-11-22 11:53:29 +01:00
probably increase performance by a lot
This commit is contained in:
parent
37f0e57868
commit
782ab181ce
6 changed files with 35 additions and 25 deletions
|
@ -1,7 +1,5 @@
|
||||||
package de.ellpeck.prettypipes.network;
|
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.misc.ItemEqualityType;
|
||||||
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
|
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
@ -12,9 +10,11 @@ import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.INBTSerializable;
|
import net.minecraftforge.common.util.INBTSerializable;
|
||||||
import net.minecraftforge.items.IItemHandler;
|
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;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class NetworkLocation implements INBTSerializable<CompoundNBT> {
|
public class NetworkLocation implements INBTSerializable<CompoundNBT> {
|
||||||
|
@ -37,17 +37,16 @@ public class NetworkLocation implements INBTSerializable<CompoundNBT> {
|
||||||
if (this.isEmpty(world))
|
if (this.isEmpty(world))
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
return this.getItems(world).entrySet().stream()
|
return this.getItems(world).entrySet().stream()
|
||||||
.filter(e -> ItemEqualityType.compareItems(e.getValue(), stack, equalityTypes))
|
.filter(kv -> ItemEqualityType.compareItems(kv.getValue(), stack, equalityTypes) && this.canExtract(world, kv.getKey()))
|
||||||
.map(Map.Entry::getKey)
|
.map(Map.Entry::getKey).collect(Collectors.toList());
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getItemAmount(World world, ItemStack stack, ItemEqualityType... equalityTypes) {
|
public int getItemAmount(World world, ItemStack stack, ItemEqualityType... equalityTypes) {
|
||||||
if (this.isEmpty(world))
|
if (this.isEmpty(world))
|
||||||
return 0;
|
return 0;
|
||||||
return this.getItems(world).values().stream()
|
return this.getItems(world).entrySet().stream()
|
||||||
.filter(i -> ItemEqualityType.compareItems(stack, i, equalityTypes))
|
.filter(kv -> ItemEqualityType.compareItems(stack, kv.getValue(), equalityTypes) && this.canExtract(world, kv.getKey()))
|
||||||
.mapToInt(ItemStack::getCount).sum();
|
.mapToInt(kv -> kv.getValue().getCount()).sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<Integer, ItemStack> getItems(World world) {
|
public Map<Integer, ItemStack> getItems(World world) {
|
||||||
|
@ -55,19 +54,25 @@ public class NetworkLocation implements INBTSerializable<CompoundNBT> {
|
||||||
IItemHandler handler = this.getItemHandler(world);
|
IItemHandler handler = this.getItemHandler(world);
|
||||||
if (handler != null) {
|
if (handler != null) {
|
||||||
for (int i = 0; i < handler.getSlots(); i++) {
|
for (int i = 0; i < handler.getSlots(); i++) {
|
||||||
|
ItemStack stack = handler.getStackInSlot(i);
|
||||||
// check if the slot is accessible to us
|
// check if the slot is accessible to us
|
||||||
if (handler.extractItem(i, 1, true).isEmpty())
|
if (stack.isEmpty())
|
||||||
continue;
|
continue;
|
||||||
if (this.itemCache == null)
|
if (this.itemCache == null)
|
||||||
this.itemCache = new HashMap<>();
|
this.itemCache = new HashMap<>();
|
||||||
// use getStackInSlot since there might be more than 64 items in there
|
// 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;
|
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) {
|
public IItemHandler getItemHandler(World world) {
|
||||||
if (this.handlerCache == null) {
|
if (this.handlerCache == null) {
|
||||||
PipeNetwork network = PipeNetwork.get(world);
|
PipeNetwork network = PipeNetwork.get(world);
|
||||||
|
|
|
@ -36,6 +36,7 @@ import net.minecraftforge.api.distmarker.Dist;
|
||||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||||
import net.minecraftforge.common.capabilities.Capability;
|
import net.minecraftforge.common.capabilities.Capability;
|
||||||
import net.minecraftforge.common.util.Constants.NBT;
|
import net.minecraftforge.common.util.Constants.NBT;
|
||||||
|
import net.minecraftforge.common.util.Lazy;
|
||||||
import net.minecraftforge.common.util.LazyOptional;
|
import net.minecraftforge.common.util.LazyOptional;
|
||||||
import net.minecraftforge.items.CapabilityItemHandler;
|
import net.minecraftforge.items.CapabilityItemHandler;
|
||||||
import net.minecraftforge.items.IItemHandler;
|
import net.minecraftforge.items.IItemHandler;
|
||||||
|
@ -76,6 +77,7 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
|
||||||
private int lastItemAmount;
|
private int lastItemAmount;
|
||||||
private int priority;
|
private int priority;
|
||||||
private final LazyOptional<PipeTileEntity> lazyThis = LazyOptional.of(() -> this);
|
private final LazyOptional<PipeTileEntity> lazyThis = LazyOptional.of(() -> this);
|
||||||
|
private final Lazy<Integer> workRandomizer = Lazy.of(() -> this.world.rand.nextInt(200));
|
||||||
|
|
||||||
public PipeTileEntity() {
|
public PipeTileEntity() {
|
||||||
this(Registry.pipeTileEntity);
|
this(Registry.pipeTileEntity);
|
||||||
|
@ -384,6 +386,10 @@ public class PipeTileEntity extends TileEntity implements INamedContainerProvide
|
||||||
this.cover = null;
|
this.cover = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean shouldWorkNow(int speed) {
|
||||||
|
return (this.world.getGameTime() + this.workRandomizer.get()) % speed == 0;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void remove() {
|
public void remove() {
|
||||||
super.remove();
|
super.remove();
|
||||||
|
|
|
@ -66,7 +66,7 @@ public class CraftingModuleItem extends ModuleItem {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick(ItemStack module, PipeTileEntity tile) {
|
public void tick(ItemStack module, PipeTileEntity tile) {
|
||||||
if (tile.getWorld().getGameTime() % this.speed != 0 || !tile.canWork())
|
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
|
||||||
return;
|
return;
|
||||||
PipeNetwork network = PipeNetwork.get(tile.getWorld());
|
PipeNetwork network = PipeNetwork.get(tile.getWorld());
|
||||||
// process crafting ingredient requests
|
// process crafting ingredient requests
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package de.ellpeck.prettypipes.pipe.modules.extraction;
|
package de.ellpeck.prettypipes.pipe.modules.extraction;
|
||||||
|
|
||||||
import de.ellpeck.prettypipes.Registry;
|
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.IModule;
|
||||||
|
import de.ellpeck.prettypipes.items.ModuleItem;
|
||||||
import de.ellpeck.prettypipes.items.ModuleTier;
|
import de.ellpeck.prettypipes.items.ModuleTier;
|
||||||
|
import de.ellpeck.prettypipes.misc.ItemFilter;
|
||||||
import de.ellpeck.prettypipes.network.PipeNetwork;
|
import de.ellpeck.prettypipes.network.PipeNetwork;
|
||||||
|
import de.ellpeck.prettypipes.pipe.PipeTileEntity;
|
||||||
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
import de.ellpeck.prettypipes.pipe.containers.AbstractPipeContainer;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.entity.player.PlayerInventory;
|
import net.minecraft.entity.player.PlayerInventory;
|
||||||
|
@ -31,9 +31,7 @@ public class ExtractionModuleItem extends ModuleItem {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick(ItemStack module, PipeTileEntity tile) {
|
public void tick(ItemStack module, PipeTileEntity tile) {
|
||||||
if (tile.getWorld().getGameTime() % this.speed != 0)
|
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
|
||||||
return;
|
|
||||||
if (!tile.canWork())
|
|
||||||
return;
|
return;
|
||||||
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
|
ItemFilter filter = new ItemFilter(this.filterSlots, module, tile);
|
||||||
|
|
||||||
|
|
|
@ -31,9 +31,7 @@ public class RetrievalModuleItem extends ModuleItem {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void tick(ItemStack module, PipeTileEntity tile) {
|
public void tick(ItemStack module, PipeTileEntity tile) {
|
||||||
if (tile.getWorld().getGameTime() % this.speed != 0)
|
if (!tile.shouldWorkNow(this.speed) || !tile.canWork())
|
||||||
return;
|
|
||||||
if (!tile.canWork())
|
|
||||||
return;
|
return;
|
||||||
PipeNetwork network = PipeNetwork.get(tile.getWorld());
|
PipeNetwork network = PipeNetwork.get(tile.getWorld());
|
||||||
|
|
||||||
|
|
|
@ -176,10 +176,13 @@ public class ItemTerminalTileEntity extends TileEntity implements INamedContaine
|
||||||
PipeTileEntity pipe = this.getConnectedPipe();
|
PipeTileEntity pipe = this.getConnectedPipe();
|
||||||
Map<EquatableItemStack, NetworkItem> items = new HashMap<>();
|
Map<EquatableItemStack, NetworkItem> items = new HashMap<>();
|
||||||
for (NetworkLocation location : network.getOrderedNetworkItems(pipe.getPos())) {
|
for (NetworkLocation location : network.getOrderedNetworkItems(pipe.getPos())) {
|
||||||
for (ItemStack stack : location.getItems(this.world).values()) {
|
for (Map.Entry<Integer, ItemStack> entry : location.getItems(this.world).entrySet()) {
|
||||||
EquatableItemStack equatable = new EquatableItemStack(stack, equalityTypes);
|
// 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);
|
NetworkItem item = items.computeIfAbsent(equatable, NetworkItem::new);
|
||||||
item.add(location, stack);
|
item.add(location, entry.getValue());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
network.endProfile();
|
network.endProfile();
|
||||||
|
|
Loading…
Reference in a new issue