Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizations for Paper (cargo network optimizations) #2106

Merged
merged 10 commits into from
Jul 21, 2020
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Small performance improvements to Energy networks
* Big performance improvements to Cargo networks when using ChestTerminal
* Slight changes to /sf timings
* Huge performance improvements when using Paper

#### Fixes
* Fixed #2075
Expand Down
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@
<pattern>io.github.thebusybiscuit.cscorelib2</pattern>
<shadedPattern>me.mrCookieSlime.Slimefun.cscorelib2</shadedPattern>
</relocation>
<relocation>
<pattern>io.papermc.lib</pattern>
<shadedPattern>io.github.thebusybiscuit.slimefun4.libraries.paperlib</shadedPattern>
</relocation>
</relocations>

<!-- Exclude unneeded metadata files from shaded dependencies -->
Expand Down Expand Up @@ -323,6 +327,12 @@
<version>1.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.papermc</groupId>
<artifactId>paperlib</artifactId>
<version>1.0.4</version>
<scope>compile</scope>
</dependency>

<!-- Third party plugin integrations -->
<dependency>
Expand All @@ -339,6 +349,7 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.HeadTexture;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.Slimefun.api.Slimefun;

Expand Down Expand Up @@ -58,10 +59,21 @@ public void openTeleporterGUI(Player p, UUID uuid, Block b, int complexity) {
}

int slot = teleporterInventory[index];

Location l = waypoint.getLocation();
menu.addItem(slot,
new CustomItem(waypoint.getIcon(), waypoint.getName().replace("player:death ", ""), "", "&8\u21E8 &7" + SlimefunPlugin.getLocalization().getResourceString(p, "tooltips.world") + ": &f" + l.getWorld().getName(), "&8\u21E8 &7X: &f" + l.getX(), "&8\u21E8 &7Y: &f" + l.getY(), "&8\u21E8 &7Z: &f" + l.getZ(), "&8\u21E8 &7" + SlimefunPlugin.getLocalization().getMessage(p, "machines.TELEPORTER.gui.time") + ": &f" + DoubleHandler.fixDouble(0.5 * getTeleportationTime(complexity, source, l)) + "s", "", "&8\u21E8 &c" + SlimefunPlugin.getLocalization().getMessage(p, "machines.TELEPORTER.gui.tooltip")));
double time = DoubleHandler.fixDouble(0.5 * getTeleportationTime(complexity, source, l));

String[] lore = {
"",
"&8\u21E8 &7" + SlimefunPlugin.getLocalization().getResourceString(p, "tooltips.world") + ": &f" + l.getWorld().getName(),
"&8\u21E8 &7X: &f" + l.getX(),
"&8\u21E8 &7Y: &f" + l.getY(),
"&8\u21E8 &7Z: &f" + l.getZ(),
"&8\u21E8 &7" + SlimefunPlugin.getLocalization().getMessage(p, "machines.TELEPORTER.gui.time") + ": &f" + time + "s",
"",
"&8\u21E8 &c" + SlimefunPlugin.getLocalization().getMessage(p, "machines.TELEPORTER.gui.tooltip")
};

menu.addItem(slot, new CustomItem(waypoint.getIcon(), waypoint.getName().replace("player:death ", ""), lore));
menu.addMenuClickHandler(slot, (pl, s, item, action) -> {
pl.closeInventory();
teleport(pl.getUniqueId(), complexity, source, l, false);
Expand Down Expand Up @@ -117,16 +129,24 @@ private void updateProgress(UUID uuid, int speed, int progress, Location source,
if (isValid(p, source)) {
if (progress > 99) {
p.sendTitle(ChatColors.color(SlimefunPlugin.getLocalization().getMessage(p, "machines.TELEPORTER.teleported")), ChatColors.color("&b100%"), 20, 60, 20);
p.teleport(destination);

if (resistance) {
p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 600, 20));
SlimefunPlugin.getLocalization().sendMessage(p, "machines.TELEPORTER.invulnerability");
}

destination.getWorld().spawnParticle(Particle.PORTAL, new Location(destination.getWorld(), destination.getX(), destination.getY() + 1, destination.getZ()), progress * 2, 0.2F, 0.8F, 0.2F);
destination.getWorld().playSound(destination, Sound.BLOCK_BEACON_ACTIVATE, 1F, 1F);
teleporterUsers.remove(uuid);
PaperLib.teleportAsync(p, destination).thenAccept(teleported -> {
if (teleported.booleanValue()) {
// This needs to run on the main Thread so we force it, as the
// async teleportation might happen on a seperate Thread.
Slimefun.runSync(() -> {
if (resistance) {
p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 600, 20));
SlimefunPlugin.getLocalization().sendMessage(p, "machines.TELEPORTER.invulnerability");
}

Location loc = new Location(destination.getWorld(), destination.getX(), destination.getY() + 1, destination.getZ());
destination.getWorld().spawnParticle(Particle.PORTAL, loc, progress * 2, 0.2F, 0.8F, 0.2F);
destination.getWorld().playSound(destination, Sound.BLOCK_BEACON_ACTIVATE, 1F, 1F);
teleporterUsers.remove(uuid);
});
}
});
}
else {
p.sendTitle(ChatColors.color(SlimefunPlugin.getLocalization().getMessage(p, "machines.TELEPORTER.teleporting")), ChatColors.color("&b" + progress + "%"), 0, 60, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.github.thebusybiscuit.slimefun4.core.commands.SlimefunCommand;
import io.github.thebusybiscuit.slimefun4.core.commands.SubCommand;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.papermc.lib.PaperLib;

class VersionsCommand extends SubCommand {

Expand All @@ -33,7 +34,10 @@ public boolean isHidden() {
@Override
public void onExecute(CommandSender sender, String[] args) {
if (sender.hasPermission("slimefun.command.versions") || sender instanceof ConsoleCommandSender) {
sender.sendMessage(ChatColors.color("&a" + Bukkit.getName() + " &2" + ReflectionUtils.getVersion()));
// After all these years... Spigot still displays as "CraftBukkit"
// so we will just fix this inconsistency for them :)
String serverSoftware = PaperLib.isSpigot() && !PaperLib.isPaper() ? "Spigot" : Bukkit.getName();
sender.sendMessage(ChatColors.color("&a" + serverSoftware + " &2" + ReflectionUtils.getVersion()));
sender.sendMessage("");
sender.sendMessage(ChatColors.color("&aCS-CoreLib &2v" + SlimefunPlugin.getCSCoreLibVersion()));
sender.sendMessage(ChatColors.color("&aSlimefun &2v" + SlimefunPlugin.getVersion()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.data.type.Dispenser;
import org.bukkit.entity.Player;
Expand All @@ -22,6 +23,7 @@
import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem;
import io.github.thebusybiscuit.slimefun4.core.handlers.MultiBlockInteractionHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
Expand Down Expand Up @@ -153,10 +155,14 @@ protected Inventory findOutputChest(Block b, ItemStack output) {

if (id != null && id.equals("OUTPUT_CHEST")) {
// Found the output chest! Now, let's check if we can fit the product in it.
Inventory inv = ((Chest) potentialOutput.getState()).getInventory();
BlockState state = PaperLib.getBlockState(potentialOutput, false).getState();

if (InvUtils.fits(inv, output)) {
return inv;
if (state instanceof Chest) {
Inventory inv = ((Chest) state).getInventory();

if (InvUtils.fits(inv, output)) {
return inv;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun;
Expand Down Expand Up @@ -93,7 +94,7 @@ static ItemStack withdraw(Map<Location, Inventory> inventories, Block node, Bloc
return withdrawFromVanillaInventory(node, template, inventory);
}

BlockState state = target.getState();
BlockState state = PaperLib.getBlockState(target, false).getState();

if (state instanceof InventoryHolder) {
inventory = ((InventoryHolder) state).getInventory();
Expand Down Expand Up @@ -181,7 +182,7 @@ else if (hasInventory(target)) {
return withdrawFromVanillaInventory(node, inventory);
}

BlockState state = target.getState();
BlockState state = PaperLib.getBlockState(target, false).getState();

if (state instanceof InventoryHolder) {
inventory = ((InventoryHolder) state).getInventory();
Expand Down Expand Up @@ -233,7 +234,7 @@ static ItemStack insert(Map<Location, Inventory> inventories, Block node, Block
return insertIntoVanillaInventory(stack, inventory);
}

BlockState state = target.getState();
BlockState state = PaperLib.getBlockState(target, false).getState();

if (state instanceof InventoryHolder) {
inventory = ((InventoryHolder) state).getInventory();
Expand All @@ -249,7 +250,7 @@ static ItemStack insert(Map<Location, Inventory> inventories, Block node, Block

for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.INSERT, wrapper)) {
ItemStack itemInSlot = menu.getItemInSlot(slot);

if (itemInSlot == null) {
menu.replaceExistingItem(slot, stack);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
Expand Down Expand Up @@ -382,7 +383,7 @@ else if (BlockStorage.hasInventory(target)) {
}
}
else if (CargoUtils.hasInventory(target)) {
BlockState state = target.getState();
BlockState state = PaperLib.getBlockState(target, false).getState();

if (state instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) state).getInventory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.tasks.ArmorTask;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.SlimefunStartupTask;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.TickerTask;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.CSCoreLib;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AGenerator;
Expand Down Expand Up @@ -164,6 +165,8 @@ public void onEnable() {
}
else if (getServer().getPluginManager().isPluginEnabled("CS-CoreLib")) {
long timestamp = System.nanoTime();

PaperLib.suggestPaper(this);

// We wanna ensure that the Server uses a compatible version of Minecraft
if (isVersionUnsupported()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.Tag;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Dispenser;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Rotatable;
Expand All @@ -36,6 +37,7 @@
import io.github.thebusybiscuit.slimefun4.utils.NumberUtils;
import io.github.thebusybiscuit.slimefun4.utils.PatternUtils;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.AdvancedMenuClickHandler;
Expand Down Expand Up @@ -689,19 +691,23 @@ else if (index < 0) {

protected void depositItems(BlockMenu menu, Block facedBlock) {
if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_ITEMS")) {
Dispenser d = (Dispenser) facedBlock.getState();
BlockState state = PaperLib.getBlockState(facedBlock, false).getState();

for (int slot : getOutputSlots()) {
ItemStack stack = menu.getItemInSlot(slot);
if (state instanceof Dispenser) {
Dispenser d = (Dispenser) state;

if (stack != null) {
Optional<ItemStack> optional = d.getInventory().addItem(stack).values().stream().findFirst();
for (int slot : getOutputSlots()) {
ItemStack stack = menu.getItemInSlot(slot);

if (optional.isPresent()) {
menu.replaceExistingItem(slot, optional.get());
}
else {
menu.replaceExistingItem(slot, null);
if (stack != null) {
Optional<ItemStack> optional = d.getInventory().addItem(stack).values().stream().findFirst();

if (optional.isPresent()) {
menu.replaceExistingItem(slot, optional.get());
}
else {
menu.replaceExistingItem(slot, null);
}
}
}
}
Expand All @@ -710,13 +716,17 @@ protected void depositItems(BlockMenu menu, Block facedBlock) {

protected void refuel(BlockMenu menu, Block facedBlock) {
if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_FUEL")) {
Dispenser d = (Dispenser) facedBlock.getState();
BlockState state = PaperLib.getBlockState(facedBlock, false).getState();

for (int slot = 0; slot < 9; slot++) {
ItemStack item = d.getInventory().getItem(slot);
if (state instanceof Dispenser) {
Dispenser d = (Dispenser) state;

if (item != null) {
insertFuel(menu, d.getInventory(), slot, menu.getItemInSlot(43), item);
for (int slot = 0; slot < 9; slot++) {
ItemStack item = d.getInventory().getItem(slot);

if (item != null) {
insertFuel(menu, d.getInventory(), slot, menu.getItemInSlot(43), item);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
Expand All @@ -24,6 +25,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
Expand Down Expand Up @@ -129,10 +131,14 @@ private Optional<Inventory> findOutputChest(Block b, ItemStack output) {

if (id != null && id.equals("OUTPUT_CHEST")) {
// Found the output chest! Now, let's check if we can fit the product in it.
Inventory inv = ((Chest) potentialOutput.getState()).getInventory();
BlockState state = PaperLib.getBlockState(potentialOutput, false).getState();

if (InvUtils.fits(inv, output)) {
return Optional.of(inv);
if (state instanceof Chest) {
Inventory inv = ((Chest) state).getInventory();

if (InvUtils.fits(inv, output)) {
return Optional.of(inv);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
Expand Down Expand Up @@ -134,8 +135,12 @@ public void open(Player p, Block b) {
yaw = -180 + (yaw - 180);
}

player.teleport(new Location(player.getWorld(), block.getX() + 0.5, block.getY() + 0.4, block.getZ() + 0.5, yaw, player.getEyeLocation().getPitch()));
player.sendTitle(ChatColor.WHITE + ChatColors.color(floor), " ", 20, 60, 20);
Location destination = new Location(player.getWorld(), block.getX() + 0.5, block.getY() + 0.4, block.getZ() + 0.5, yaw, player.getEyeLocation().getPitch());
PaperLib.teleportAsync(player, destination).thenAccept(teleported -> {
if (teleported.booleanValue()) {
player.sendTitle(ChatColor.WHITE + ChatColors.color(floor), null, 20, 60, 20);
}
});
})));
}

Expand Down
Loading