1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Small improvement to Cargo performance

This commit is contained in:
TheBusyBiscuit 2020-07-05 12:04:40 +02:00
parent 4edc557547
commit 88dfdfa560
5 changed files with 30 additions and 18 deletions

View File

@ -8,13 +8,12 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.network.Network;
@ -274,7 +273,8 @@ public class CargoNet extends ChestTerminalNetwork {
}
private void routeItems(Location inputNode, Block inputTarget, int frequency, Map<Integer, List<Location>> outputNodes) {
ItemStackAndInteger slot = CargoUtils.withdraw(inputNode.getBlock(), inputTarget);
AtomicReference<Object> inventory = new AtomicReference<>();
ItemStackAndInteger slot = CargoUtils.withdraw(inputNode.getBlock(), inputTarget, inventory);
if (slot == null) {
return;
@ -289,9 +289,11 @@ public class CargoNet extends ChestTerminalNetwork {
}
if (stack != null) {
DirtyChestMenu menu = CargoUtils.getChestMenu(inputTarget);
Object inputInventory = inventory.get();
if (inputInventory instanceof DirtyChestMenu) {
DirtyChestMenu menu = (DirtyChestMenu) inputInventory;
if (menu != null) {
if (menu.getItemInSlot(previousSlot) == null) {
menu.replaceExistingItem(previousSlot, stack);
}
@ -299,18 +301,15 @@ public class CargoNet extends ChestTerminalNetwork {
inputTarget.getWorld().dropItem(inputTarget.getLocation().add(0, 1, 0), stack);
}
}
else if (CargoUtils.hasInventory(inputTarget)) {
BlockState state = inputTarget.getState();
if (state instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) state).getInventory();
if (inputInventory instanceof Inventory) {
Inventory inv = (Inventory) inputInventory;
if (inv.getItem(previousSlot) == null) {
inv.setItem(previousSlot, stack);
}
else {
inputTarget.getWorld().dropItem(inputTarget.getLocation().add(0, 1, 0), stack);
}
if (inv.getItem(previousSlot) == null) {
inv.setItem(previousSlot, stack);
}
else {
inputTarget.getWorld().dropItem(inputTarget.getLocation().add(0, 1, 0), stack);
}
}
}

View File

@ -2,6 +2,7 @@ package io.github.thebusybiscuit.slimefun4.core.networks.cargo;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import org.bukkit.Material;
@ -153,7 +154,7 @@ final class CargoUtils {
return null;
}
static ItemStackAndInteger withdraw(Block node, Block target) {
static ItemStackAndInteger withdraw(Block node, Block target, AtomicReference<Object> inventory) {
DirtyChestMenu menu = getChestMenu(target);
if (menu != null) {
@ -162,6 +163,7 @@ final class CargoUtils {
if (matchesFilter(node, is)) {
menu.replaceExistingItem(slot, null);
inventory.set(menu);
return new ItemStackAndInteger(is, slot);
}
}
@ -189,6 +191,7 @@ final class CargoUtils {
if (matchesFilter(node, is)) {
inv.setItem(slot, null);
inventory.set(inv);
return new ItemStackAndInteger(is, slot);
}
}

View File

@ -12,6 +12,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import org.bukkit.Location;
import org.bukkit.Material;
@ -194,7 +195,7 @@ abstract class ChestTerminalNetwork extends Network {
Optional<Block> target = getAttachedBlock(bus.getBlock());
if (target.isPresent()) {
ItemStackAndInteger stack = CargoUtils.withdraw(bus.getBlock(), target.get());
ItemStackAndInteger stack = CargoUtils.withdraw(bus.getBlock(), target.get(), new AtomicReference<>());
if (stack != null) {
menu.replaceExistingItem(17, stack.getItem());

View File

@ -24,7 +24,8 @@ public enum PerformanceRating implements Predicate<Float> {
OKAY(ChatColor.GREEN, 30),
MODERATE(ChatColor.YELLOW, 55),
SEVERE(ChatColor.RED, 85),
HURTFUL(ChatColor.DARK_RED, Float.MAX_VALUE);
HURTFUL(ChatColor.DARK_RED, 500),
BAD(ChatColor.DARK_RED, Float.MAX_VALUE);
private final ChatColor color;
private final float threshold;

View File

@ -231,6 +231,10 @@ public final class Slimefun {
return null;
}
if (SlimefunPlugin.instance == null || !SlimefunPlugin.instance.isEnabled()) {
return null;
}
return Bukkit.getScheduler().runTask(SlimefunPlugin.instance, r);
}
@ -240,6 +244,10 @@ public final class Slimefun {
return null;
}
if (SlimefunPlugin.instance == null || !SlimefunPlugin.instance.isEnabled()) {
return null;
}
return Bukkit.getScheduler().runTaskLater(SlimefunPlugin.instance, r, delay);
}
}