Skip to content

Commit b3db843

Browse files
committed
Implement worker capability
1 parent 672f6c8 commit b3db843

11 files changed

Lines changed: 127 additions & 32 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/api/java/org/cyclops/commoncapabilities/api"]
2+
path = src/api/java/org/cyclops/commoncapabilities/api
3+
url = https://github.com/CyclopsMC/CommonCapabilitiesAPI.git

build.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod_version=0.1.2
22
minecraft_version=1.8.9
3-
forge_version=11.15.0.1716
4-
cyclopscore_version=0.5.0-226
3+
forge_version=11.15.1.1722
4+
cyclopscore_version=0.5.1-246
55
release_type=release
Submodule api added at 4142c03
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.cyclops.structuredcrafting;
2+
3+
import net.minecraftforge.common.capabilities.Capability;
4+
import net.minecraftforge.common.capabilities.CapabilityInject;
5+
import org.cyclops.commoncapabilities.api.capability.work.IWorker;
6+
7+
/**
8+
* Used capabilities for this mod.
9+
* @author rubensworks
10+
*/
11+
public class Capabilities {
12+
@CapabilityInject(IWorker.class)
13+
public static Capability<IWorker> WORKER = null;
14+
}

src/main/java/org/cyclops/structuredcrafting/StructuredCrafting.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
package org.cyclops.structuredcrafting;
22

33
import net.minecraft.creativetab.CreativeTabs;
4+
import net.minecraftforge.common.capabilities.Capability;
45
import net.minecraftforge.fml.common.Mod;
56
import net.minecraftforge.fml.common.event.*;
67
import org.apache.logging.log4j.Level;
8+
import org.cyclops.commoncapabilities.api.capability.work.IWorker;
79
import org.cyclops.cyclopscore.config.ConfigHandler;
810
import org.cyclops.cyclopscore.config.extendedconfig.BlockItemConfigReference;
911
import org.cyclops.cyclopscore.init.ItemCreativeTab;
1012
import org.cyclops.cyclopscore.init.ModBaseVersionable;
1113
import org.cyclops.cyclopscore.init.RecipeHandler;
14+
import org.cyclops.cyclopscore.modcompat.ICapabilityCompat;
15+
import org.cyclops.cyclopscore.modcompat.ModCompatLoader;
1216
import org.cyclops.cyclopscore.proxy.ICommonProxy;
1317
import org.cyclops.structuredcrafting.block.BlockStructuredCrafterConfig;
1418
import org.cyclops.structuredcrafting.craft.provider.IItemStackProviderRegistry;
1519
import org.cyclops.structuredcrafting.craft.provider.InventoryItemStackProvider;
1620
import org.cyclops.structuredcrafting.craft.provider.ItemStackProviderRegistry;
1721
import org.cyclops.structuredcrafting.craft.provider.WorldItemStackProvider;
22+
import org.cyclops.structuredcrafting.modcompat.capabilities.WorkerStructuredCrafterTileCompat;
23+
import org.cyclops.structuredcrafting.tileentity.TileStructuredCrafter;
1824

1925
/**
2026
* The main mod class of StructuredCrafting.
@@ -45,6 +51,20 @@ protected RecipeHandler constructRecipeHandler() {
4551
return new RecipeHandler(this, "recipes.xml");
4652
}
4753

54+
@Override
55+
protected void loadModCompats(ModCompatLoader modCompatLoader) {
56+
super.loadModCompats(modCompatLoader);
57+
58+
// Capabilities
59+
ICapabilityCompat.ICapabilityReference<IWorker> workerReference = new ICapabilityCompat.ICapabilityReference<IWorker>() {
60+
@Override
61+
public Capability<IWorker> getCapability() {
62+
return Capabilities.WORKER;
63+
}
64+
};
65+
modCompatLoader.addCapabilityCompat(TileStructuredCrafter.class, workerReference, new WorkerStructuredCrafterTileCompat());
66+
}
67+
4868
@Mod.EventHandler
4969
@Override
5070
public final void preInit(FMLPreInitializationEvent event) {

src/main/java/org/cyclops/structuredcrafting/craft/WorldCraftingMatrix.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ protected Pair<ItemStack, IItemStackProvider> determineItemStackProviderForInput
6363
return null;
6464
}
6565

66-
protected boolean addItemStackForOutput(World world, BlockPos pos, EnumFacing side, List<IItemStackProvider> outputProviders, ItemStack itemStack) {
66+
protected boolean addItemStackForOutput(World world, BlockPos pos, EnumFacing side, List<IItemStackProvider> outputProviders, ItemStack itemStack, boolean simulate) {
6767
for(IItemStackProvider provider : outputProviders) {
68-
if(provider.addItemStack(world, pos, side, itemStack)) {
68+
if(provider.addItemStack(world, pos, side, itemStack, simulate)) {
6969
return true;
7070
}
7171
}
7272
return false;
7373
}
7474

75-
public boolean craft() {
75+
public boolean craft(boolean simulate) {
7676
// Check if at least one of the providers can write to the output target.
7777
List<IItemStackProvider> outputProviders = Lists.newLinkedList();
7878
for(IItemStackProvider provider : getItemStackProviders()) {
@@ -117,15 +117,15 @@ public boolean craft() {
117117
}
118118

119119
// Determine output
120-
if(itemStack != null && addItemStackForOutput(world, targetPos, targetSide, outputProviders, itemStack)) {
120+
if(itemStack != null && addItemStackForOutput(world, targetPos, targetSide, outputProviders, itemStack, simulate)) {
121121
// Handle remaining container items: place blocks and drop items
122122
ItemStack[] remainingStacks = CraftingManager.getInstance().func_180303_b(INVENTORY_CRAFTING, world);
123123
for(int i = 0; i < remainingStacks.length; i++) {
124124
ItemStack remainingStack = remainingStacks[i];
125125
if(providers[i] != null) {
126-
providers[i].reduceItemStack(world, positions[i], inputSide);
126+
providers[i].reduceItemStack(world, positions[i], inputSide, simulate);
127127
if (remainingStack != null && remainingStack.stackSize > 0) {
128-
providers[i].addItemStack(world, positions[i], inputSide, remainingStack);
128+
providers[i].addItemStack(world, positions[i], inputSide, remainingStack, simulate);
129129
}
130130
}
131131
}

src/main/java/org/cyclops/structuredcrafting/craft/provider/IItemStackProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,30 @@ public interface IItemStackProvider {
4343
* @param world The world.
4444
* @param pos The position.
4545
* @param side The side.
46+
* @param simulate If the operation should be simulated.
4647
*/
47-
public void reduceItemStack(World world, BlockPos pos, EnumFacing side);
48+
public void reduceItemStack(World world, BlockPos pos, EnumFacing side, boolean simulate);
4849

4950
/**
5051
* Adds an itemstack.
5152
* @param world The world.
5253
* @param pos The position.
5354
* @param side The side.
5455
* @param itemStack The itemstack to set.
56+
* @param simulate If the operation should be simulated.
5557
* @return If the insertion succeeded.
5658
*/
57-
public boolean addItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack);
59+
public boolean addItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack, boolean simulate);
5860

5961
/**
6062
* Set the itemstack.
6163
* @param world The world.
6264
* @param pos The position.
6365
* @param side The side.
6466
* @param itemStack The itemstack to set.
67+
* @param simulate If the operation should be simulated.
6568
* @return If the insertion succeeded.
6669
*/
67-
public boolean setItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack);
70+
public boolean setItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack, boolean simulate);
6871

6972
}

src/main/java/org/cyclops/structuredcrafting/craft/provider/InventoryItemStackProvider.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
import net.minecraft.inventory.IInventory;
44
import net.minecraft.item.ItemStack;
5-
import net.minecraft.tileentity.TileEntity;
65
import net.minecraft.util.BlockPos;
76
import net.minecraft.util.EnumFacing;
87
import net.minecraft.world.World;
9-
import net.minecraftforge.common.capabilities.Capability;
108
import net.minecraftforge.items.CapabilityItemHandler;
119
import net.minecraftforge.items.IItemHandler;
1210
import org.apache.commons.lang3.tuple.Pair;
@@ -62,11 +60,11 @@ public ItemStack getItemStack(World world, BlockPos pos, EnumFacing side) {
6260
}
6361

6462
@Override
65-
public void reduceItemStack(World world, BlockPos pos, EnumFacing side) {
63+
public void reduceItemStack(World world, BlockPos pos, EnumFacing side, boolean simulate) {
6664
IItemHandler itemHandler = TileHelpers.getCapability(world, pos, side, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
6765
if(itemHandler != null) {
6866
for(int slot = 0; slot < itemHandler.getSlots(); slot++) {
69-
if(itemHandler.extractItem(slot, 1, false) != null) {
67+
if(itemHandler.extractItem(slot, 1, simulate) != null) {
7068
break;
7169
}
7270
}
@@ -78,23 +76,25 @@ public void reduceItemStack(World world, BlockPos pos, EnumFacing side) {
7876
if (newItemStack.stackSize <= 0) {
7977
newItemStack = null;
8078
}
81-
inventory.setInventorySlotContents(result.getLeft(), newItemStack);
79+
if(!simulate) {
80+
inventory.setInventorySlotContents(result.getLeft(), newItemStack);
81+
}
8282
}
8383
}
8484

8585
@Override
86-
public boolean addItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack) {
86+
public boolean addItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack, boolean simulate) {
8787
IItemHandler itemHandler = TileHelpers.getCapability(world, pos, side, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
8888
if(itemHandler != null) {
8989
for(int slot = 0; slot < itemHandler.getSlots(); slot++) {
90-
if(itemHandler.insertItem(slot, itemStack, false) == null) {
90+
if(itemHandler.insertItem(slot, itemStack, simulate) == null) {
9191
return true;
9292
}
9393
}
9494
} else {
9595
IInventory inventory = TileHelpers.getSafeTile(world, pos, IInventory.class);
9696
for (int slot = 0; slot < inventory.getSizeInventory(); slot++) {
97-
if (InventoryHelpers.addToSlot(inventory, slot, itemStack)) {
97+
if (InventoryHelpers.addToSlot(inventory, slot, itemStack, simulate)) {
9898
return true;
9999
}
100100
}
@@ -103,19 +103,21 @@ public boolean addItemStack(World world, BlockPos pos, EnumFacing side, ItemStac
103103
}
104104

105105
@Override
106-
public boolean setItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack) {
106+
public boolean setItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack, boolean simulate) {
107107
IItemHandler itemHandler = TileHelpers.getCapability(world, pos, side, CapabilityItemHandler.ITEM_HANDLER_CAPABILITY);
108108
if(itemHandler != null) {
109109
for(int slot = 0; slot < itemHandler.getSlots(); slot++) {
110-
if(itemHandler.insertItem(slot, itemStack, false) == null) {
110+
if(itemHandler.insertItem(slot, itemStack, simulate) == null) {
111111
return true;
112112
}
113113
}
114114
} else {
115115
IInventory inventory = TileHelpers.getSafeTile(world, pos, IInventory.class);
116116
Pair<Integer, ItemStack> result = getFirstItem(inventory, side);
117117
if (result != null) {
118-
inventory.setInventorySlotContents(result.getLeft(), itemStack);
118+
if(!simulate) {
119+
inventory.setInventorySlotContents(result.getLeft(), itemStack);
120+
}
119121
return true;
120122
}
121123
}

src/main/java/org/cyclops/structuredcrafting/craft/provider/WorldItemStackProvider.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,24 @@ public ItemStack getItemStack(World world, BlockPos pos, EnumFacing side) {
3939
}
4040

4141
@Override
42-
public void reduceItemStack(World world, BlockPos pos, EnumFacing side) {
43-
world.setBlockToAir(pos);
42+
public void reduceItemStack(World world, BlockPos pos, EnumFacing side, boolean simulate) {
43+
if(!simulate) {
44+
world.setBlockToAir(pos);
45+
}
4446
}
4547

4648
@Override
47-
public boolean addItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack) {
48-
return setItemStack(world, pos, side, itemStack);
49+
public boolean addItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack, boolean simulate) {
50+
return setItemStack(world, pos, side, itemStack, simulate);
4951
}
5052

5153
@Override
52-
public boolean setItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack) {
53-
if(itemStack.getItem() instanceof ItemBlock) {
54+
public boolean setItemStack(World world, BlockPos pos, EnumFacing side, ItemStack itemStack, boolean simulate) {
55+
if(!simulate && itemStack.getItem() instanceof ItemBlock) {
5456
world.setBlockState(pos, ((ItemBlock) itemStack.getItem()).getBlock().getStateFromMeta(itemStack.getItemDamage()));
5557
itemStack.stackSize--;
5658
}
57-
if(itemStack.stackSize > 0) {
59+
if(!simulate && itemStack.stackSize > 0) {
5860
ItemStackHelpers.spawnItemStack(world, pos, itemStack);
5961
}
6062
return true;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.cyclops.structuredcrafting.modcompat.capabilities;
2+
3+
import org.cyclops.commoncapabilities.api.capability.work.IWorker;
4+
import org.cyclops.cyclopscore.modcompat.ICapabilityCompat;
5+
import org.cyclops.structuredcrafting.Capabilities;
6+
import org.cyclops.structuredcrafting.tileentity.TileStructuredCrafter;
7+
8+
/**
9+
* Compatibility for structured crafter worker capability.
10+
* @author rubensworks
11+
*/
12+
public class WorkerStructuredCrafterTileCompat implements ICapabilityCompat<TileStructuredCrafter> {
13+
14+
@Override
15+
public void attach(final TileStructuredCrafter provider) {
16+
provider.addCapabilityInternal(Capabilities.WORKER, new Worker(provider));
17+
}
18+
19+
public static class Worker implements IWorker {
20+
21+
private final TileStructuredCrafter provider;
22+
23+
public Worker(TileStructuredCrafter provider) {
24+
this.provider = provider;
25+
}
26+
27+
@SuppressWarnings("unchecked")
28+
@Override
29+
public boolean hasWork() {
30+
return provider.getMatrix().craft(true);
31+
}
32+
33+
@Override
34+
public boolean canWork() {
35+
return provider.getWorld().isBlockPowered(provider.getPos());
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)