Skip to content

Commit 9da4dde

Browse files
committed
Port to Fabric
1 parent e7076ca commit 9da4dde

8 files changed

Lines changed: 311 additions & 1 deletion

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ org.gradle.daemon=false
4242
org.gradle.caching=true
4343

4444
# Common dependencies
45-
cyclopscore_version=1.21.0-585
45+
cyclopscore_version=1.21.1-593
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package org.cyclops.structuredcrafting;
2+
3+
import net.fabricmc.api.ModInitializer;
4+
import net.minecraft.world.item.CreativeModeTab;
5+
import net.minecraft.world.item.ItemStack;
6+
import org.cyclops.cyclopscore.config.ConfigHandlerCommon;
7+
import org.cyclops.cyclopscore.init.ModBaseFabric;
8+
import org.cyclops.cyclopscore.proxy.IClientProxyCommon;
9+
import org.cyclops.cyclopscore.proxy.ICommonProxyCommon;
10+
import org.cyclops.structuredcrafting.block.BlockStructuredCrafterConfig;
11+
import org.cyclops.structuredcrafting.blockentity.BlockEntityStructuredCrafterConfig;
12+
import org.cyclops.structuredcrafting.craft.provider.IItemStackProviderRegistry;
13+
import org.cyclops.structuredcrafting.craft.provider.InventoryItemStackProviderFabric;
14+
import org.cyclops.structuredcrafting.craft.provider.ItemStackProviderRegistry;
15+
import org.cyclops.structuredcrafting.craft.provider.WorldItemStackProviderFabric;
16+
import org.cyclops.structuredcrafting.proxy.ClientProxyFabric;
17+
import org.cyclops.structuredcrafting.proxy.CommonProxyFabric;
18+
19+
/**
20+
* The main mod class of this mod.
21+
* @author rubensworks
22+
*/
23+
public class StructuredCraftingFabric extends ModBaseFabric<StructuredCraftingFabric> implements ModInitializer, IStructuredCraftingMod {
24+
25+
/**
26+
* The unique instance of this mod.
27+
*/
28+
public static StructuredCraftingFabric _instance;
29+
30+
public StructuredCraftingFabric() {
31+
super(Reference.MOD_ID, (instance) -> {
32+
_instance = instance;
33+
IStructuredCraftingMod.MOD.set(instance);
34+
});
35+
}
36+
37+
@Override
38+
protected IClientProxyCommon constructClientProxy() {
39+
return new ClientProxyFabric();
40+
}
41+
42+
@Override
43+
protected ICommonProxyCommon constructCommonProxy() {
44+
return new CommonProxyFabric();
45+
}
46+
47+
@Override
48+
protected boolean hasDefaultCreativeModeTab() {
49+
return true;
50+
}
51+
52+
@Override
53+
protected CreativeModeTab.Builder constructDefaultCreativeModeTab(CreativeModeTab.Builder builder) {
54+
return super.constructDefaultCreativeModeTab(builder)
55+
.icon(() -> new ItemStack(RegistryEntries.ITEM_STRUCTURED_CRAFTER));
56+
}
57+
58+
@Override
59+
public void onInitialize() {
60+
getRegistryManager().addRegistry(IItemStackProviderRegistry.class, new ItemStackProviderRegistry());
61+
62+
super.onInitialize();
63+
64+
IItemStackProviderRegistry registry = getRegistryManager().getRegistry(IItemStackProviderRegistry.class);
65+
registry.registerProvider(new InventoryItemStackProviderFabric());
66+
registry.registerProvider(new WorldItemStackProviderFabric());
67+
}
68+
69+
@Override
70+
protected void onConfigsRegister(ConfigHandlerCommon configHandler) {
71+
super.onConfigsRegister(configHandler);
72+
73+
configHandler.addConfigurable(new GeneralConfig(this));
74+
75+
configHandler.addConfigurable(new BlockStructuredCrafterConfig<>(this));
76+
configHandler.addConfigurable(new BlockEntityStructuredCrafterConfig<>(this));
77+
}
78+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.cyclops.structuredcrafting.craft.provider;
2+
3+
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
4+
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
5+
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
6+
import net.fabricmc.fabric.api.transfer.v1.storage.StorageView;
7+
import net.fabricmc.fabric.api.transfer.v1.transaction.Transaction;
8+
import net.minecraft.core.BlockPos;
9+
import net.minecraft.core.Direction;
10+
import net.minecraft.world.item.ItemStack;
11+
import net.minecraft.world.level.Level;
12+
import org.cyclops.structuredcrafting.block.BlockStructuredCrafterConfig;
13+
14+
import java.util.Iterator;
15+
16+
/**
17+
* Inventory that can provide itemstacks.
18+
* @author rubensworks
19+
*/
20+
public class InventoryItemStackProviderFabric implements IItemStackProvider {
21+
22+
@Override
23+
public boolean canProvideInput() {
24+
return BlockStructuredCrafterConfig.canTakeInputsFromInventory;
25+
}
26+
27+
@Override
28+
public boolean canHandleOutput() {
29+
return BlockStructuredCrafterConfig.canPlaceOutputsIntoInventory;
30+
}
31+
32+
@Override
33+
public boolean isValidForResults(Level world, BlockPos pos, Direction side) {
34+
return ItemStorage.SIDED.find(world, pos, side) != null;
35+
}
36+
37+
@Override
38+
public boolean hasItemStack(Level world, BlockPos pos, Direction side) {
39+
return ItemStorage.SIDED.find(world, pos, side) != null;
40+
}
41+
42+
@Override
43+
public ItemStack getItemStack(Level world, BlockPos pos, Direction side) {
44+
Storage<ItemVariant> storage = ItemStorage.SIDED.find(world, pos, side);
45+
if (storage != null) {
46+
Iterator<StorageView<ItemVariant>> it = storage.nonEmptyIterator();
47+
if (it.hasNext()) {
48+
return it.next().getResource().toStack();
49+
}
50+
}
51+
return ItemStack.EMPTY;
52+
}
53+
54+
@Override
55+
public boolean reduceItemStack(Level world, BlockPos pos, Direction side, boolean simulate) {
56+
Storage<ItemVariant> storage = ItemStorage.SIDED.find(world, pos, side);
57+
if (storage != null) {
58+
Iterator<StorageView<ItemVariant>> it = storage.nonEmptyIterator();
59+
if (it.hasNext()) {
60+
ItemStack firstStack = it.next().getResource().toStack();
61+
try (Transaction tx = Transaction.openOuter()) {
62+
long extracted = storage.extract(ItemVariant.of(firstStack), 1, tx);
63+
if (simulate) {
64+
tx.abort();
65+
} else {
66+
tx.commit();
67+
}
68+
return extracted > 0;
69+
}
70+
}
71+
}
72+
return false;
73+
}
74+
75+
@Override
76+
public boolean addItemStack(Level world, BlockPos pos, Direction side, ItemStack itemStack, boolean simulate) {
77+
Storage<ItemVariant> storage = ItemStorage.SIDED.find(world, pos, side);
78+
if (storage != null) {
79+
try (Transaction tx = Transaction.openOuter()) {
80+
long inserted = storage.insert(ItemVariant.of(itemStack), 1, tx);
81+
if (simulate) {
82+
tx.abort();
83+
} else {
84+
tx.commit();
85+
}
86+
return inserted > 0;
87+
}
88+
}
89+
return false;
90+
}
91+
92+
@Override
93+
public boolean setItemStack(Level world, BlockPos pos, Direction side, ItemStack itemStack, boolean simulate) {
94+
return addItemStack(world, pos, side, itemStack, simulate);
95+
}
96+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.cyclops.structuredcrafting.craft.provider;
2+
3+
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
4+
import net.fabricmc.fabric.api.transfer.v1.item.ItemVariant;
5+
import net.fabricmc.fabric.api.transfer.v1.storage.Storage;
6+
import net.minecraft.core.BlockPos;
7+
import net.minecraft.core.Direction;
8+
import net.minecraft.world.item.ItemStack;
9+
import net.minecraft.world.level.Level;
10+
import net.minecraft.world.level.block.state.BlockState;
11+
12+
/**
13+
* World that can provide an itemstack.
14+
* @author rubensworks
15+
*/
16+
public class WorldItemStackProviderFabric extends WorldItemStackProviderBase {
17+
18+
@Override
19+
protected boolean hasEmptyItemHandler(Level world, BlockPos pos, Direction side) {
20+
Storage<ItemVariant> itemHandler = ItemStorage.SIDED.find(world, pos, side);
21+
if (itemHandler != null) {
22+
return !itemHandler.nonEmptyIterator().hasNext();
23+
}
24+
return true;
25+
}
26+
27+
@Override
28+
public ItemStack getItemStack(Level world, BlockPos pos, Direction side) {
29+
BlockState blockState = world.getBlockState(pos);
30+
if(blockState != null && hasEmptyItemHandler(world, pos, side)) {
31+
return blockState.getBlock().getCloneItemStack(world, pos, blockState);
32+
}
33+
return ItemStack.EMPTY;
34+
}
35+
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.cyclops.structuredcrafting.proxy;
2+
3+
import org.cyclops.cyclopscore.init.ModBaseFabric;
4+
import org.cyclops.cyclopscore.proxy.ClientProxyComponentFabric;
5+
import org.cyclops.structuredcrafting.StructuredCraftingFabric;
6+
7+
/**
8+
* Proxy for the client side.
9+
*
10+
* @author rubensworks
11+
*
12+
*/
13+
public class ClientProxyFabric extends ClientProxyComponentFabric {
14+
15+
public ClientProxyFabric() {
16+
super(new CommonProxyFabric());
17+
}
18+
19+
@Override
20+
public ModBaseFabric<?> getMod() {
21+
return StructuredCraftingFabric._instance;
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.cyclops.structuredcrafting.proxy;
2+
3+
import org.cyclops.cyclopscore.init.ModBaseFabric;
4+
import org.cyclops.cyclopscore.proxy.CommonProxyComponentFabric;
5+
import org.cyclops.structuredcrafting.StructuredCraftingFabric;
6+
7+
/**
8+
* Proxy for server and client side.
9+
* @author rubensworks
10+
*
11+
*/
12+
public class CommonProxyFabric extends CommonProxyComponentFabric {
13+
14+
@Override
15+
public ModBaseFabric<?> getMod() {
16+
return StructuredCraftingFabric._instance;
17+
}
18+
19+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"type": "minecraft:crafting_shaped",
3+
"category": "redstone",
4+
"pattern": [
5+
"SCS",
6+
"IRI",
7+
"SCS"
8+
],
9+
"key": {
10+
"S": {
11+
"item": "minecraft:stick"
12+
},
13+
"C": {
14+
"tag": "forge:cobblestone"
15+
},
16+
"I": {
17+
"tag": "forge:ingots/iron"
18+
},
19+
"R": {
20+
"item": "minecraft:crafting_table"
21+
}
22+
},
23+
"result": {
24+
"id": "structuredcrafting:structured_crafter"
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"schemaVersion": 1,
3+
"id": "${mod_id}",
4+
"version": "${mod_version}",
5+
6+
"name": "${mod_name}",
7+
"description": "${description}",
8+
"authors": [
9+
"${mod_author}"
10+
],
11+
"contact": {
12+
"homepage": "${display_url}",
13+
"sources": "${issue_tracker_url}"
14+
},
15+
16+
"license": "${license}",
17+
"icon": "logo.png",
18+
19+
"environment": "*",
20+
"entrypoints": {
21+
"main": [
22+
"org.cyclops.structuredcrafting.StructuredCraftingFabric"
23+
]
24+
},
25+
26+
"depends": {
27+
"cyclopscore": ">=${cyclopscore_version_semver}",
28+
"fabricloader": ">=${fabric_loader_version}",
29+
"fabric-api": ">=${fabric_version}",
30+
"java": ">=${java_version}"
31+
}
32+
}

0 commit comments

Comments
 (0)