Skip to content

Commit

Permalink
Fixes and performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sefiraat committed Jan 15, 2022
1 parent b57045c commit 19d17b2
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 50 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.sefiraat</groupId>
<artifactId>networks</artifactId>
<version>MODIFIED_1.0.2</version>
<version>MODIFIED_1.0.3</version>
<build>
<sourceDirectory>${basedir}/src/main/java</sourceDirectory>
<defaultGoal>clean package</defaultGoal>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.sefiraat</groupId>
<artifactId>networks</artifactId>
<version>MODIFIED_1.0.2</version>
<version>MODIFIED_1.0.3</version>

<distributionManagement>
<repository>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
package io.github.sefiraat.networks.network.stackcaches;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

public class BlueprintInstance extends ItemStackCache {

private final ItemStack[] recipe;
private final ItemStack[] recipeItems;
@Nullable
private Recipe recipe = null;

public BlueprintInstance(@Nonnull ItemStack[] recipe, @Nonnull ItemStack expectedOutput) {
public BlueprintInstance(@Nonnull ItemStack[] recipeItems, @Nonnull ItemStack expectedOutput) {
super(expectedOutput);
this.recipe = recipe;
this.recipeItems = recipeItems;
}

public ItemStack[] getRecipeItems() {
return recipeItems;
}

public ItemStack[] getRecipe() {
@Nullable
public Recipe getRecipe() {
return recipe;
}

public void setRecipe(@Nullable Recipe recipe) {
this.recipe = recipe;
}

public void generateVanillaRecipe(World world) {
if (this.recipe == null) {
this.recipe = Bukkit.getCraftingRecipe(this.recipeItems, world);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public class NetworksSlimefunItemStacks {

NETWORK_CAPACITOR_2 = Theme.themedSlimefunItemStack(
"NTW_CAPACITOR_2",
new ItemStack(Material.LIGHT_BLUE_GLAZED_TERRACOTTA),
new ItemStack(Material.GREEN_GLAZED_TERRACOTTA),
Theme.MACHINE,
"Network Capacitor (2)",
"The Network Capacitor can take",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem;
import io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack;
import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.Interaction;
Expand All @@ -28,21 +27,18 @@
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.meta.ItemMeta;

import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;

public class NetworkAutoCrafter extends NetworkObject {

Expand All @@ -65,7 +61,8 @@ public class NetworkAutoCrafter extends NetworkObject {

private final int chargePerCraft;
private final boolean directSubmit;
private static final Map<Location, UUID> OWNER_MAP = new HashMap<>();

private static final Map<Location, BlueprintInstance> INSTANCE_MAP = new HashMap<>();

public NetworkAutoCrafter(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, int chargePerCraft, boolean directSubmit) {
super(itemGroup, item, recipeType, recipe, NodeType.CRAFTER);
Expand All @@ -91,25 +88,13 @@ public void tick(Block block, SlimefunItem slimefunItem, Config config) {
craftPreFlight(blockMenu);
}
}
},
new BlockPlaceHandler(false) {
@Override
public void onPlayerPlace(@Nonnull BlockPlaceEvent event) {
final UUID uuid = event.getPlayer().getUniqueId();
BlockStorage.addBlockInfo(event.getBlock(), "owner", uuid.toString());
OWNER_MAP.put(event.getBlock().getLocation(), uuid);
}
}
);
}

protected void craftPreFlight(@Nonnull BlockMenu blockMenu) {

final Player player = Bukkit.getPlayer(OWNER_MAP.get(blockMenu.getLocation()));

if (player == null) {
return;
}
releaseCache(blockMenu);

final NodeDefinition definition = NetworkStorage.getAllNetworkObjects().get(blockMenu.getLocation());

Expand All @@ -133,14 +118,20 @@ protected void craftPreFlight(@Nonnull BlockMenu blockMenu) {
return;
}

final ItemMeta blueprintMeta = blueprint.getItemMeta();
final Optional<BlueprintInstance> optional = DataTypeMethods.getOptionalCustom(blueprintMeta, Keys.BLUEPRINT_INSTANCE, PersistentCraftingBlueprintType.TYPE);
BlueprintInstance instance = INSTANCE_MAP.get(blockMenu.getLocation());

if (optional.isEmpty()) {
return;
if (instance == null) {
final ItemMeta blueprintMeta = blueprint.getItemMeta();
final Optional<BlueprintInstance> optional = DataTypeMethods.getOptionalCustom(blueprintMeta, Keys.BLUEPRINT_INSTANCE, PersistentCraftingBlueprintType.TYPE);

if (optional.isEmpty()) {
return;
}

instance = optional.get();
setCache(blockMenu, instance);
}

final BlueprintInstance instance = optional.get();
final ItemStack outputItem = blockMenu.getItemInSlot(OUTPUT_SLOT);

if (outputItem != null
Expand All @@ -150,24 +141,27 @@ protected void craftPreFlight(@Nonnull BlockMenu blockMenu) {
return;
}

if (tryCraft(blockMenu, player, instance, root)) {
if (tryCraft(blockMenu, instance, root)) {
root.removeCharge(this.chargePerCraft);
}

if (this.directSubmit) {
root.addItemStack(blockMenu.getItemInSlot(OUTPUT_SLOT));
final ItemStack crafted = blockMenu.getItemInSlot(OUTPUT_SLOT);
if (crafted != null && crafted.getType() != Material.AIR) {
root.addItemStack(crafted);
}
}
}
}

private boolean tryCraft(@Nonnull BlockMenu blockMenu, @Nonnull Player player, @Nonnull BlueprintInstance instance, @Nonnull NetworkRoot root) {
private boolean tryCraft(@Nonnull BlockMenu blockMenu, @Nonnull BlueprintInstance instance, @Nonnull NetworkRoot root) {
// Get the recipe input
final ItemStack[] inputs = new ItemStack[9];

for (int i = 0; i < 9; i++) {
final ItemStack requested = instance.getRecipe()[i];
final ItemStack requested = instance.getRecipeItems()[i];
if (requested != null) {
final ItemStack fetched = root.getItemStack(new ItemRequest(instance.getRecipe()[i], 1));
final ItemStack fetched = root.getItemStack(new ItemRequest(instance.getRecipeItems()[i], 1));
inputs[i] = fetched;
} else {
inputs[i] = null;
Expand All @@ -186,23 +180,43 @@ private boolean tryCraft(@Nonnull BlockMenu blockMenu, @Nonnull Player player, @

// If no slimefun recipe found, try a vanilla one
if (crafted == null) {
final Recipe recipe = Bukkit.getCraftingRecipe(inputs, blockMenu.getBlock().getWorld());
crafted = recipe == null ? null : recipe.getResult();
instance.generateVanillaRecipe(blockMenu.getLocation().getWorld());
if (instance.getRecipe() == null) {
return false;
}
setCache(blockMenu, instance);
crafted = instance.getRecipe().getResult();
}

// If no item crafted OR result doesn't fit, escape
if (crafted == null || crafted.getType() == Material.AIR) {
if (crafted.getType() == Material.AIR) {
for (ItemStack input : inputs) {
root.addItemStack(input);
if (input != null) {
root.addItemStack(input);
}
}
return false;
}

// Push item
final Location location = blockMenu.getLocation().clone().add(0.5, 1.1, 0.5);
location.getWorld().spawnParticle(Particle.WAX_OFF, location, 0, 0, 4, 0);
blockMenu.pushItem(crafted, OUTPUT_SLOT);
return true;
}

public void releaseCache(@Nonnull BlockMenu blockMenu) {
if (blockMenu.hasViewer()) {
INSTANCE_MAP.remove(blockMenu.getLocation());
}
}

public void setCache(@Nonnull BlockMenu blockMenu, @Nonnull BlueprintInstance blueprintInstance) {
if (!blockMenu.hasViewer()) {
INSTANCE_MAP.putIfAbsent(blockMenu.getLocation(), blueprintInstance);
}
}


@Override
public void postRegister() {
Expand All @@ -215,19 +229,12 @@ public void init() {
drawBackground(OUTPUT_BACKGROUND_STACK, OUTPUT_BACKGROUND);
}


@Override
public boolean canOpen(@Nonnull Block block, @Nonnull Player player) {
return NetworkSlimefunItems.NETWORK_AUTO_CRAFTER.canUse(player, false)
&& Slimefun.getProtectionManager().hasPermission(player, block.getLocation(), Interaction.INTERACT_BLOCK);
}

@Override
public void newInstance(@Nonnull BlockMenu menu, @Nonnull Block b) {
String owner = BlockStorage.getLocationInfo(b.getLocation(), "owner");
OWNER_MAP.put(b.getLocation(), UUID.fromString(owner));
}

@Override
public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) {
return new int[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import java.util.UUID;

public class NetworkPowerDisplay extends NetworkObject {

private static final int[] BACKGROUND_SLOTS = new int[]{
0,1,2,3,5,6,7,8
0, 1, 2, 3, 5, 6, 7, 8
};
private static final int DISPLAY_SLOT = 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public Class<BlueprintInstance> getComplexType() {
public PersistentDataContainer toPrimitive(@Nonnull BlueprintInstance complex, @Nonnull PersistentDataAdapterContext context) {
final PersistentDataContainer container = context.newPersistentDataContainer();

container.set(RECIPE, DataType.ITEM_STACK_ARRAY, complex.getRecipe());
container.set(RECIPE, DataType.ITEM_STACK_ARRAY, complex.getRecipeItems());
container.set(OUTPUT, DataType.ITEM_STACK, complex.getItemStack());
return container;
}
Expand Down

0 comments on commit 19d17b2

Please sign in to comment.