Skip to content

Commit

Permalink
last touches on crafters
Browse files Browse the repository at this point in the history
  • Loading branch information
Sefiraat committed Jan 15, 2022
1 parent 19d17b2 commit 5a99c27
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 25 deletions.
81 changes: 78 additions & 3 deletions src/main/java/io/github/sefiraat/networks/network/NetworkRoot.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.item_transport.ItemTransportFlow;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -53,8 +54,6 @@ public class NetworkRoot extends NetworkNode {
protected final Set<Location> networkCrafters = new HashSet<>();
protected Set<BarrelIdentity> barrels = null;

private long totalPower = 0;

public NetworkRoot(Location location, NodeType type) {
super(location, type);
this.root = this;
Expand Down Expand Up @@ -131,10 +130,15 @@ public Set<Location> getPurgers() {
return networkPurgers;
}

public Set<Location> getCrafters() {
return networkCrafters;
}

@Nonnull
public Map<ItemStack, Integer> getAllNetworkItems() {
final Map<ItemStack, Integer> itemStacks = new HashMap<>();

// Barrels
for (BarrelIdentity barrelIdentity : getBarrels()) {
final Integer currentAmount = itemStacks.get(barrelIdentity.getItemStack());
final int newAmount;
Expand All @@ -151,6 +155,30 @@ public Map<ItemStack, Integer> getAllNetworkItems() {
itemStacks.put(barrelIdentity.getItemStack(), newAmount);
}

for (BlockMenu blockMenu : getCrafterOutputs()) {
int[] slots = blockMenu.getPreset().getSlotsAccessedByItemTransport(ItemTransportFlow.WITHDRAW);
for (int slot : slots) {
final ItemStack itemStack = blockMenu.getItemInSlot(slot);
if (itemStack == null || itemStack.getType() == Material.AIR) {
continue;
}
final ItemStack clone = StackUtils.getAsQuantity(itemStack, 1);
final Integer currentAmount = itemStacks.get(clone);
final int newAmount;
if (currentAmount == null) {
newAmount = itemStack.getAmount();
} else {
long newLong = (long) currentAmount + (long) itemStack.getAmount();
if (newLong > Integer.MAX_VALUE) {
newAmount = Integer.MAX_VALUE;
} else {
newAmount = currentAmount + itemStack.getAmount();
}
}
itemStacks.put(clone, newAmount);
}
}

for (BlockMenu blockMenu : getCellMenus()) {
for (ItemStack itemStack : blockMenu.getContents()) {
if (itemStack != null && itemStack.getType() != Material.AIR) {
Expand Down Expand Up @@ -326,10 +354,23 @@ public Set<BlockMenu> getCellMenus() {
return menus;
}

@Nonnull
public Set<BlockMenu> getCrafterOutputs() {
final Set<BlockMenu> menus = new HashSet<>();
for (Location location : networkCrafters) {
BlockMenu menu = BlockStorage.getInventory(location);
if (menu != null) {
menus.add(menu);
}
}
return menus;
}

@Nullable
public ItemStack getItemStack(@Nonnull ItemRequest request) {
ItemStack stackToReturn = null;

// Cells first
for (BlockMenu blockMenu : getCellMenus()) {
for (ItemStack itemStack : blockMenu.getContents()) {
if (itemStack == null
Expand Down Expand Up @@ -365,14 +406,48 @@ public ItemStack getItemStack(@Nonnull ItemRequest request) {
}
}

// Crafters
for (BlockMenu blockMenu : getCrafterOutputs()) {
int[] slots = blockMenu.getPreset().getSlotsAccessedByItemTransport(ItemTransportFlow.WITHDRAW);
for (int slot : slots) {
final ItemStack itemStack = blockMenu.getItemInSlot(slot);
if (itemStack == null || itemStack.getType() == Material.AIR || !StackUtils.itemsMatch(request, itemStack)) {
continue;
}

// Stack is null, so we can fill it here
if (stackToReturn == null) {
stackToReturn = itemStack.clone();
stackToReturn.setAmount(1);
request.receiveAmount(1);
itemStack.setAmount(itemStack.getAmount() - 1);
}

// Escape if fulfilled request
if (request.getAmount() <= 0) {
return stackToReturn;
}

if (request.getAmount() <= itemStack.getAmount()) {
stackToReturn.setAmount(stackToReturn.getAmount() + request.getAmount());
itemStack.setAmount(itemStack.getAmount() - request.getAmount());
return stackToReturn;
} else {
stackToReturn.setAmount(stackToReturn.getAmount() + itemStack.getAmount());
request.receiveAmount(itemStack.getAmount());
itemStack.setAmount(0);
}
}
}

// Barrels
for (BarrelIdentity barrelIdentity : getBarrels()) {
if (barrelIdentity.holdsMatchingItem(request.getItemStack())) {
final ItemStack itemStack = barrelIdentity.requestItem(request.getItemStack());
boolean infinity = barrelIdentity instanceof InfinityBarrel;

if (itemStack == null
|| (infinity && itemStack.getAmount() == 1)
//|| !SlimefunUtils.isItemSimilar(request.getItemStack(), itemStack, true, false)
|| !StackUtils.itemsMatch(request, itemStack)
) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ public class NetworkSlimefunItems {
OPTIC_CABLE.getItem(), SIMPLE_NANOBOTS.getItem(), OPTIC_CABLE.getItem(),
OPTIC_GLASS.getItem(), SlimefunItems.ENHANCED_AUTO_CRAFTER, OPTIC_GLASS.getItem(),
},
1000,
true
64,
false
);

NETWORK_AUTO_CRAFTER_WITHHOLDING = new NetworkAutoCrafter(
Expand All @@ -403,8 +403,8 @@ public class NetworkSlimefunItems {
OPTIC_CABLE.getItem(), ADVANCED_NANOBOTS.getItem(), OPTIC_CABLE.getItem(),
OPTIC_GLASS.getItem(), NETWORK_AUTO_CRAFTER.getItem(), OPTIC_GLASS.getItem(),
},
2000,
false
128,
true
);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public class NetworksSlimefunItemStacks {
"it will be crafted if you have",
"materials.",
"",
MessageFormat.format("{0}Network Drain: {1}{2}/craft", Theme.CLICK_INFO, Theme.PASSIVE, 1000)
MessageFormat.format("{0}Network Drain: {1}{2}/craft", Theme.CLICK_INFO, Theme.PASSIVE, 64)
);

NETWORK_AUTO_CRAFTER_WITHHOLDING = Theme.themedSlimefunItemStack(
Expand All @@ -385,10 +385,11 @@ public class NetworksSlimefunItemStacks {
"materials.",
"A Withholding Crafter will keep",
"a stack in the output and stop",
"crafting. The stack can bee seen",
"in the Network.",
"crafting. The stack can been seen",
"in the Network and also allows for",
"cargo.",
"",
MessageFormat.format("{0}Network Drain: {1}{2}/craft", Theme.CLICK_INFO, Theme.PASSIVE, 2000)
MessageFormat.format("{0}Network Drain: {1}{2}/craft", Theme.CLICK_INFO, Theme.PASSIVE, 128)
);

NETWORK_MEMORY_CARD_1 = Theme.themedSlimefunItemStack(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public class NetworkAutoCrafter extends NetworkObject {
);

private final int chargePerCraft;
private final boolean directSubmit;
private final boolean withholding;

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

public NetworkAutoCrafter(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, int chargePerCraft, boolean directSubmit) {
public NetworkAutoCrafter(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, int chargePerCraft, boolean withholding) {
super(itemGroup, item, recipeType, recipe, NodeType.CRAFTER);

this.chargePerCraft = chargePerCraft;
this.directSubmit = directSubmit;
this.withholding = withholding;

this.getSlotsToDrop().add(BLUEPRINT_SLOT);
this.getSlotsToDrop().add(OUTPUT_SLOT);
Expand Down Expand Up @@ -98,17 +98,25 @@ protected void craftPreFlight(@Nonnull BlockMenu blockMenu) {

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

if (definition.getNode() == null) {
if (definition == null || definition.getNode() == null) {
return;
}

final NetworkRoot root = definition.getNode().getRoot();

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

final ItemStack blueprint = blockMenu.getItemInSlot(BLUEPRINT_SLOT);

if (blueprint == null || blueprint.getType() == Material.AIR) {
return;
}

final NetworkRoot root = definition.getNode().getRoot();
final long networkCharge = root.getDownstreamCharge();

if (networkCharge > this.chargePerCraft) {
Expand Down Expand Up @@ -136,21 +144,14 @@ protected void craftPreFlight(@Nonnull BlockMenu blockMenu) {

if (outputItem != null
&& outputItem.getType() != Material.AIR
&& (!StackUtils.itemsMatch(instance, outputItem) || outputItem.getAmount() >= outputItem.getMaxStackSize())
) {
&& (!StackUtils.itemsMatch(instance, outputItem) || outputItem.getAmount() >= outputItem.getMaxStackSize()))
{
return;
}

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

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

Expand Down Expand Up @@ -237,6 +238,9 @@ public boolean canOpen(@Nonnull Block block, @Nonnull Player player) {

@Override
public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) {
if (NetworkAutoCrafter.this.withholding && flow == ItemTransportFlow.WITHDRAW) {
return new int[]{OUTPUT_SLOT};
}
return new int[0];
}
};
Expand Down

0 comments on commit 5a99c27

Please sign in to comment.