Skip to content

Commit

Permalink
I am pleased to announce that I have gone mad with power
Browse files Browse the repository at this point in the history
  • Loading branch information
LemmaEOF committed Aug 28, 2020
1 parent 25592c5 commit 9e891a3
Show file tree
Hide file tree
Showing 20 changed files with 400 additions and 127 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
include "io.github.cottonmc:Parchment:${project.parchment_version}"
include "io.github.cottonmc:LibCD:${project.libcd_version}"

modCompileOnly ("com.github.Siphalor:nbt-crafting:1.16-2.0-SNAPSHOT") { transitive = false }
// modCompileOnly ("com.github.Siphalor:nbt-crafting:1.16-2.0-SNAPSHOT") { transitive = false }
// modRuntime ("com.github.Siphalor:nbt-crafting:1.16-2.0-SNAPSHOT") { transitive = false } //only for testing
}

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ yarn_build=19
loader_version=0.9.1+build.205

# Mod Properties
mod_version = 1.0.0
mod_version = 1.1.0
maven_group = io.github.cottonmc
archives_base_name = libdp

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.17.2+build.396-1.16
parchment_version=1.1.0+1.16.2
libcd_version=3.0.1+1.16.2
libcd_version=3.0.2+1.16.2
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

@Mixin(ReferenceLootCondition.class)
public interface ReferenceLootConditionAccessor {
@Invoker("<init>")
static ReferenceLootCondition callConstructor(Identifier id) {
throw new AssertionError();
}
@Invoker("<init>")
static ReferenceLootCondition callConstructor(Identifier id) {
throw new AssertionError();
}
}
18 changes: 14 additions & 4 deletions src/main/java/io/github/cottonmc/libdp/LibDP.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.cottonmc.libdp;

import java.nio.file.Files;

import com.mojang.brigadier.tree.LiteralCommandNode;
import io.github.cottonmc.libdp.api.DriverInitializer;
import io.github.cottonmc.libdp.api.driver.DriverManager;
Expand All @@ -11,7 +13,6 @@
import org.apache.logging.log4j.Logger;

import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.SpecialRecipeSerializer;
import net.minecraft.resource.ResourceType;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -28,15 +29,24 @@ public class LibDP implements ModInitializer {

public static final Logger LOGGER = LogManager.getLogger(MODID);

public static RecipeSerializer<CustomSpecialCraftingRecipe> CUSTOM_SPECIAL_SERIALIZER;
public static RecipeSerializer<CustomSpecialCraftingRecipe> CUSTOM_SPECIAL_SERIALIZER = new CustomSpecialCraftingRecipe.Serializer();

//if true, custom special recipes will not register their serializer and will lie about their identity during sync
//WARNING: CURRENTLY UNTESTED
public static boolean COMPATIBILITY_MODE = false;

@Override
public void onInitialize() {
//TODO: real config
COMPATIBILITY_MODE = Boolean.getBoolean("libdp.compat")
|| Files.exists(FabricLoader.getInstance().getConfigDir().resolve("libdp_compat_mode.txt"));
FabricLoader.getInstance().getEntrypoints(MODID, DriverInitializer.class).forEach(init ->
init.init(DriverManager.INSTANCE));
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new DisketteLoader());
CUSTOM_SPECIAL_SERIALIZER = Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(MODID,
"custom_special_crafting"), new SpecialRecipeSerializer<>(CustomSpecialCraftingRecipe::new));
if (!COMPATIBILITY_MODE) {
Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(MODID,
"custom_special_crafting"), CUSTOM_SPECIAL_SERIALIZER);
}
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {

//New nodes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import net.minecraft.world.World;

public class CustomShapedRecipe extends ShapedRecipe {
private Diskette bridge;
private Logger logger;
private final Diskette diskette;
private final Logger logger;

public CustomShapedRecipe(Diskette bridge, Identifier id, String group, int width, int height, DefaultedList<Ingredient> ingredients, ItemStack output) {
public CustomShapedRecipe(Diskette diskette, Identifier id, String group, int width, int height, DefaultedList<Ingredient> ingredients, ItemStack output) {
super(id, group, width, height, ingredients, output);
this.bridge = bridge;
this.logger = LogManager.getLogger(bridge.getId().toString());
this.diskette = diskette;
this.logger = LogManager.getLogger(diskette.getId().toString());
}

@Override
Expand All @@ -31,7 +31,7 @@ public boolean matches(CraftingInventory inv, World world) {
if (!matches) return false;
try {
PlayerEntity player = CraftingUtils.findPlayer(inv);
Object result = bridge.invokeFunction("matches", CraftingUtils.getInvStacks(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new WorldInfo(world));
Object result = diskette.invokeFunction("matches", CraftingUtils.getInvStacks2d(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new WorldInfo(world));
if (result instanceof Boolean) return (Boolean) result;
else {
logger.error("Could not check match for custom shaped recipe {}, returning standard match: function 'matches' must return a boolean, but returned {} instead", getId(), result.getClass().getName());
Expand All @@ -49,7 +49,8 @@ public ItemStack craft(CraftingInventory inv) {
try {
MutableStack mutableStack = new MutableStack(stack);
PlayerEntity player = CraftingUtils.findPlayer(inv);
Object result = bridge.invokeFunction("preview", CraftingUtils.getInvStacks(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, mutableStack ); return result == null? mutableStack.get() : RecipeParser.processItemStack(result);
Object result = diskette.invokeFunction("preview", CraftingUtils.getInvStacks2d(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, mutableStack );
return result == null? mutableStack.get() : RecipeParser.processItemStack(result);
} catch (Exception e) {
logger.error("Could not get preview output for custom shaped recipe {}, returning standard output: {}", getId(), e.getMessage());
return super.craft(inv);
Expand All @@ -61,9 +62,9 @@ public DefaultedList<ItemStack> getRemainingStacks(CraftingInventory inv) {
DefaultedList<ItemStack> remainingStacks = super.getRemainingStacks(inv);
try {
PlayerEntity player = CraftingUtils.findPlayer(inv);
bridge.invokeFunction("craft", CraftingUtils.getInvStacks(inv), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new StackInfo(craft(inv)));
diskette.invokeFunction("craft", CraftingUtils.getInvStacks2d(inv), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new StackInfo(craft(inv)));
} catch (Exception e) {
logger.error("Could not fully craft custom shaped recipe %s, ignoring: %s", getId(), e.getMessage());
logger.error("Could not fully craft custom shaped recipe {}, ignoring: {}", getId(), e.getMessage());
}
return remainingStacks;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import net.minecraft.world.World;

public class CustomShapelessRecipe extends ShapelessRecipe {
private Diskette bridge;
private Logger logger;
private final Diskette diskette;
private final Logger logger;

public CustomShapelessRecipe(Diskette bridge, Identifier id, String group, DefaultedList<Ingredient> ingredients, ItemStack output) {
public CustomShapelessRecipe(Diskette diskette, Identifier id, String group, DefaultedList<Ingredient> ingredients, ItemStack output) {
super(id, group, output, ingredients);
this.bridge = bridge;
this.logger = LogManager.getLogger(bridge.getId().toString());
this.diskette = diskette;
this.logger = LogManager.getLogger(diskette.getId().toString());
}

@Override
Expand All @@ -31,7 +31,7 @@ public boolean matches(CraftingInventory inv, World world) {
if (!matches) return false;
try {
PlayerEntity player = CraftingUtils.findPlayer(inv);
Object result = bridge.invokeFunction("matches", CraftingUtils.getInvStacks(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new WorldInfo(world));
Object result = diskette.invokeFunction("matches", CraftingUtils.getInvStacks2d(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new WorldInfo(world));
if (result instanceof Boolean) return (Boolean) result;
else {
logger.error("Could not check match for custom shapeless recipe {}, returning standard match: function 'matches' must return a boolean, but returned {} instead", getId(), result.getClass().getName());
Expand All @@ -49,10 +49,10 @@ public ItemStack craft(CraftingInventory inv) {
try {
MutableStack mutableStack = new MutableStack(stack);
PlayerEntity player = CraftingUtils.findPlayer(inv);
Object result = bridge.invokeFunction("preview", CraftingUtils.getInvStacks(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, mutableStack );
Object result = diskette.invokeFunction("preview", CraftingUtils.getInvStacks2d(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, mutableStack);
return result == null? mutableStack.get() : RecipeParser.processItemStack(result);
} catch (Exception e) {
logger.error("Could not get preview output for custom shapeless recipe %s, returning standard output: %s", getId(), e.getMessage());
logger.error("Could not get preview output for custom shapeless recipe {}, returning standard output: {}", getId(), e.getMessage());
return super.craft(inv);
}
}
Expand All @@ -62,9 +62,9 @@ public DefaultedList<ItemStack> getRemainingStacks(CraftingInventory inv) {
DefaultedList<ItemStack> remainingStacks = super.getRemainingStacks(inv);
try {
PlayerEntity player = CraftingUtils.findPlayer(inv);
bridge.invokeFunction("craft", CraftingUtils.getInvStacks(inv), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new StackInfo(craft(inv)));
diskette.invokeFunction("craft", CraftingUtils.getInvStacks2d(inv), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new StackInfo(craft(inv)));
} catch (Exception e) {
logger.error("Could not fully craft custom shapeless recipe %s, ignoring: %s", getId(), e.getMessage());
logger.error("Could not fully craft custom shapeless recipe {}, ignoring: {}", getId(), e.getMessage());
}
return remainingStacks;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,52 @@
package io.github.cottonmc.libdp.api.driver.recipe;

import com.google.gson.JsonObject;
import io.github.cottonmc.libdp.LibDP;
import io.github.cottonmc.libdp.api.Diskette;
import io.github.cottonmc.libdp.api.util.DummyPlayer;
import io.github.cottonmc.libdp.api.util.StackInfo;
import io.github.cottonmc.libdp.api.util.WorldInfo;
import io.github.cottonmc.libdp.api.util.WrappedPlayer;
import io.github.cottonmc.libdp.api.util.crafting.CraftingUtils;
import io.github.cottonmc.libdp.loader.DisketteLoader;
import io.github.cottonmc.libdp.loader.NullDiskette;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.SpecialCraftingRecipe;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;

public class CustomSpecialCraftingRecipe extends SpecialCraftingRecipe {
private Diskette bridge;
private Logger logger;
private final Diskette diskette;
private final Logger logger;

public CustomSpecialCraftingRecipe(Diskette bridge, Identifier id) {
super(id);
this.bridge = bridge;
this.logger = LogManager.getLogger(bridge.getId().toString());
}

public CustomSpecialCraftingRecipe(Identifier id) {
public CustomSpecialCraftingRecipe(Diskette diskette, Identifier id) {
super(id);
this.diskette = diskette;
this.logger = LogManager.getLogger(diskette.getId().toString());
}

@Override
public boolean matches(CraftingInventory inv, World world) {
try {
PlayerEntity player = CraftingUtils.findPlayer(inv);
Object result = bridge.invokeFunction("matches", CraftingUtils.getInvStacks(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new WorldInfo(world));
Object result = diskette.invokeFunction("matches", CraftingUtils.getInvStacks2d(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new WorldInfo(world));
if (result instanceof Boolean) return (Boolean) result;
else {
logger.error("Could not check match for custom special crafting recipe {}, returning false: function 'matches' must returna boolean, but returned {} instead", getId(), result.getClass().getName());
logger.error("Could not check match for custom special crafting recipe {}, returning false: function 'matches' must return a boolean, but returned {} instead", getId(), result.getClass().getName());
return false;
}
} catch (Exception e) {
logger.error("Could not check match for custom special crafting recipe {}, returning false: {}", getId(), e.getMessage());
e.printStackTrace();
}
return false;
}
Expand All @@ -53,15 +55,15 @@ public boolean matches(CraftingInventory inv, World world) {
public ItemStack craft(CraftingInventory inv) {
try {
PlayerEntity player = CraftingUtils.findPlayer(inv);
Object result = bridge.invokeFunction("preview", CraftingUtils.getInvStacks(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE);
Object result = diskette.invokeFunction("preview", CraftingUtils.getInvStacks2d(inv), inv.getWidth(), inv.getHeight(), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE);
if (result == null) {
logger.error("Could not get preview output for custom special crafting recipe %s, returning empty stack: function 'preview' must not return null", getId());
logger.error("Could not get preview output for custom special crafting recipe {}, returning empty stack: function 'preview' must not return null", getId());
return ItemStack.EMPTY;
} else {
return RecipeParser.processItemStack(result);
}
} catch (Exception e) {
logger.error("Could not get preview output for custom special crafting recipe %s, returning empty: %s", getId(), e.getMessage());
logger.error("Could not get preview output for custom special crafting recipe {}, returning empty stack: {}", getId(), e.getMessage());
return ItemStack.EMPTY;
}
}
Expand All @@ -71,22 +73,38 @@ public boolean fits(int width, int height) {
return true; //this doesn't matter, since it's a special crafting recipe
}

//TODO: make sure this is only called on server?
//this *should* only be called on server, from what I can tell
@Override
public DefaultedList<ItemStack> getRemainingStacks(CraftingInventory inv) {
DefaultedList<ItemStack> remainingStacks = super.getRemainingStacks(inv);
try {
PlayerEntity player = CraftingUtils.findPlayer(inv);
bridge.invokeFunction("craft", CraftingUtils.getInvStacks(inv), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new StackInfo(craft(inv)));
diskette.invokeFunction("craft", CraftingUtils.getInvStacks2d(inv), player != null? new WrappedPlayer(player) : DummyPlayer.INSTANCE, new StackInfo(craft(inv)));
} catch (Exception e) {
logger.error("Could not fully craft custom special crafting recipe %s, ignoring: %s", getId(), e.getMessage());
logger.error("Could not fully craft custom special crafting recipe {}, ignoring: {}", getId(), e.getMessage());
}
return remainingStacks;
}

//TODO: custom serializer to let users specify a script bridge?
@Override
public RecipeSerializer<?> getSerializer() {
public RecipeSerializer<CustomSpecialCraftingRecipe> getSerializer() {
return LibDP.CUSTOM_SPECIAL_SERIALIZER;
}

public static class Serializer implements RecipeSerializer<CustomSpecialCraftingRecipe> {

@Override
public CustomSpecialCraftingRecipe read(Identifier id, JsonObject json) {
Diskette diskette = DisketteLoader.DISKETTES.get(new Identifier(JsonHelper.getString(json, "diskette")));
return new CustomSpecialCraftingRecipe(diskette, id);
}

@Override
public CustomSpecialCraftingRecipe read(Identifier id, PacketByteBuf buf) {
return new CustomSpecialCraftingRecipe(NullDiskette.INSTANCE, id);
}

@Override
public void write(PacketByteBuf buf, CustomSpecialCraftingRecipe recipe) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ else if (input instanceof MutableStack) {
((IngredientAccessUtils) (Object) ing).libdp$setMatchType(NbtMatchType.EXACT);
}
return ing;
}
else if (input instanceof ItemStack) {
} else if (input instanceof Item) {
return Ingredient.ofItems((Item) input);
} else if (input instanceof Item[]) {
return Ingredient.ofItems((Item[]) input);
} else if (input instanceof ItemStack) {
ItemStack stack = (ItemStack) input;
Ingredient ing = hackStackIngredients(stack);
if (stack.hasTag()) {
Expand Down Expand Up @@ -102,7 +105,7 @@ else if (input instanceof ItemStack) {
stacks.add(stack);
type = NbtMatchType.EXACT;
} else {
Item item = DriverUtils.INSTANCE.getItem(in);
Item item = DriverUtils.INSTANCE.getRawItem(in);
if (item == Items.AIR) throw new DPSyntaxError("Failed to get item for input: " + in);
stacks.add(new ItemStack(item));
}
Expand All @@ -123,6 +126,7 @@ else if (input instanceof ItemStack) {
public static ItemStack processItemStack(Object input) throws DPSyntaxError {
if (input instanceof ItemStack) return (ItemStack) input;
else if (input instanceof MutableStack) return ((MutableStack) input).get();
else if (input instanceof Item) return (new ItemStack((Item) input));
else if (input instanceof String) {
String in = (String) input;
int atIndex = in.lastIndexOf('@');
Expand Down Expand Up @@ -152,7 +156,7 @@ else if (input instanceof String) {
}
return stack;
} else {
item = DriverUtils.INSTANCE.getItem(in);
item = DriverUtils.INSTANCE.getRawItem(in);
}

ItemStack stack = new ItemStack(item, count);
Expand Down
Loading

0 comments on commit 9e891a3

Please sign in to comment.