Skip to content

Commit

Permalink
KubeJS support, 1x1 table fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaTheDinkus committed Jul 25, 2020
1 parent 42451d8 commit 8a03bbd
Show file tree
Hide file tree
Showing 12 changed files with 549 additions and 23 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
modImplementation("me.shedaniel:RoughlyEnoughItems:${project.rei_version}") { exclude module: "ModMenu" }
modImplementation "com.github.Siphalor:nbt-crafting:${project.nbtcrafting_version}"
modImplementation "com.lettuce.fudge:artifice:${project.artifice_version}"
modImplementation "com.github.KubeJS-Mods:KubeJS-Fabric:${project.kubejs_version}"

include "io.github.cottonmc:LibGui:${project.libgui_version}"
include "io.github.cottonmc:StaticData:${project.static_data_version}"
Expand Down
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ loader_version=0.9.0+build.204
fabric_version=0.15.1+build.380-1.16.1

# Mod Properties
mod_version = 1.4.2
mod_version = 1.4.3
maven_group = io.github.alloffabric
archives_base_name = artis

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
# libcd_version=2.5.0+1.16.1
kubejs_version=master-SNAPSHOT
libgui_version=2.2.0+1.16.1
static_data_version=1.1.2
rei_version=4.10.3
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/io/github/alloffabric/artis/Artis.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
package io.github.alloffabric.artis;

import com.mojang.serialization.Lifecycle;
import dev.latvian.kubejs.script.ScriptType;
import io.github.alloffabric.artis.api.ArtisExistingBlockType;
import io.github.alloffabric.artis.api.ArtisExistingItemType;
import io.github.alloffabric.artis.api.ArtisTableType;
import io.github.alloffabric.artis.block.ArtisTableBEBlock;
import io.github.alloffabric.artis.block.ArtisTableBlock;
import io.github.alloffabric.artis.block.ArtisTableItem;
import io.github.alloffabric.artis.block.entity.ArtisTableBlockEntity;
import io.github.alloffabric.artis.compat.kubejs.ArtisJsonRegistryEventJS;
import io.github.alloffabric.artis.event.ArtisEvents;
import io.github.alloffabric.artis.inventory.ArtisCraftingController;
import io.github.alloffabric.artis.util.ArtisRegistry;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
import net.fabricmc.fabric.api.network.ClientSidePacketRegistry;
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.fabricmc.fabric.impl.screenhandler.ExtendedScreenHandlerType;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.Recipe;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -85,6 +84,9 @@ private static <B extends BlockEntity> BlockEntityType<B> registerBlockEntity(St
@Override
public void onInitialize() {
if (!isLoaded) {
if (FabricLoader.getInstance().isModLoaded("kubejs"))
new ArtisJsonRegistryEventJS().post(ScriptType.STARTUP, "artis.registry");

ArtisData.loadData();
ArtisData.loadConfig();
ArtisEvents.init();
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/io/github/alloffabric/artis/ArtisData.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import io.github.cottonmc.jankson.JanksonFactory;
import io.github.cottonmc.staticdata.StaticData;
import io.github.cottonmc.staticdata.StaticDataItem;
import net.fabricmc.fabric.api.block.FabricBlockSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
Expand Down Expand Up @@ -60,6 +60,12 @@ public static void loadData() {
}
}

public static void loadData(JsonObject json) {
if (json != null) {
loadEntries("kubejs", json.containsKey("tables") ? json.getObject("tables") : json);
}
}

private static void loadEntries(String from, JsonObject json) {
List<String> keys = new ArrayList<>(json.keySet());
Collections.sort(keys);
Expand All @@ -76,7 +82,7 @@ private static void loadEntries(String from, JsonObject json) {
if (config.containsKey("settings")) {
settings = BlockSettingsParser.parseSettings(config.getObject("settings"));
} else {
settings = FabricBlockSettings.copy(Blocks.CRAFTING_TABLE).build();
settings = FabricBlockSettings.copyOf(Blocks.CRAFTING_TABLE);
}
Artis.registerTable(type, settings);
}
Expand All @@ -89,13 +95,19 @@ static ArtisTableType getType(String key, JsonObject json) {
String tableType = json.containsKey("type") ? json.get(String.class, "type") : "normal";
int width = json.getInt("width", 3);
int height = json.getInt("height", 3);
if (width > 9) {
Artis.logger.error("[Artis] Table type named {} has too many columns, clamping it to 9", key);
width = 9;
if (width > 7) {
Artis.logger.warn("[Artis] Only tables up to 7 columns are supported. Anything higher may, and likely will, break visually.", key);
if (width > 9) {
Artis.logger.error("[Artis] Table type named {} has too many columns, clamping it to 9", key);
width = 9;
}
}
if (height > 9) {
Artis.logger.error("[Artis] Table type named {} has too many rows, clamping it to 9", key);
height = 9;
if (height > 7) {
Artis.logger.warn("[Artis] Only tables up to 7 rows are supported. Anything higher may, and likely will, break visually.", key);
if (height > 9) {
Artis.logger.error("[Artis] Table type named {} has too many rows, clamping it to 9", key);
height = 9;
}
}
boolean blockEntity = json.getBoolean("block_entity", false);
boolean catalystSlot = json.getInt("catalyst_slot", 0) != 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ public class ContainerLayout {
private int arrowX;
private final int arrowY;
private final int playerX;
private final int playerY;
private int playerY;

public ContainerLayout(int gridColumns, int gridRows) {
public ContainerLayout(int gridColumns, int gridRows, boolean hasCatalyst) {
this.gridWidth = gridColumns * 18;
this.catalystX = gridWidth + 10;
this.resultX = catalystX + 18 + 14;
this.resultX = catalystX + 18 + 10;
this.arrowX = catalystX - 3;
this.craftingWidth = resultX + 22;

Expand All @@ -33,7 +33,7 @@ public ContainerLayout(int gridColumns, int gridRows) {
this.arrowY = midpoint + 4;
this.catalystY = arrowY + 21;
this.resultY = midpoint + 4;
this.craftingHeight = Math.max(35, gridHeight);
this.craftingHeight = Math.max(25, gridHeight);

this.gridY = 16;
this.gridX = craftingWidth > playerWidth ? 0 : ((playerWidth - craftingWidth) / 2) + 2;
Expand All @@ -43,6 +43,11 @@ public ContainerLayout(int gridColumns, int gridRows) {

this.playerX = playerWidth > craftingWidth ? 0 : ((craftingWidth - playerWidth) / 2) + 2;
this.playerY = gridY + Math.max(48, gridHeight + 10);
if (!hasCatalyst && gridRows == 1) {
this.playerY -= 18;
} else if (hasCatalyst && gridRows == 1) {
this.playerY -= 8;
}
}

public int getGridX() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package io.github.alloffabric.artis.compat.kubejs;

import blue.endless.jankson.api.SyntaxError;
import com.google.gson.JsonObject;
import dev.latvian.kubejs.KubeJS;
import dev.latvian.kubejs.util.BuilderBase;
import io.github.alloffabric.artis.Artis;
import io.github.alloffabric.artis.ArtisData;
import net.minecraft.util.Identifier;

public class ArtisJsonBuilder extends BuilderBase {
public JsonObject root;
public JsonObject tables;
public JsonObject table;
public JsonObject settings;

public ArtisJsonBuilder(String i) {
super(i);
root = new JsonObject();
tables = new JsonObject();
table = new JsonObject();
settings = new JsonObject();
root.add("tables", tables);
tables.add(KubeJS.appendModId(i), table);
table.add("settings", settings);
}

public ArtisJsonBuilder(String modId, String name) {
super(name);
root = new JsonObject();
tables = new JsonObject();
table = new JsonObject();
settings = new JsonObject();
root.add("tables", tables);
tables.add(new Identifier(modId, name).toString(), table);
table.add("settings", settings);
}

public ArtisJsonBuilder setType(String type) {
table.addProperty("type", type);
return this;
}

public ArtisJsonBuilder dimensions(int width, int height) {
table.addProperty("width", width);
table.addProperty("height", height);
return this;
}

public ArtisJsonBuilder generateAssets() {
table.addProperty("generate_assets", true);
return this;
}

public ArtisJsonBuilder color(String color) {
table.addProperty("color", color);
return this;
}

public ArtisJsonBuilder blockEntity() {
table.addProperty("block_entity", true);
return this;
}

public ArtisJsonBuilder catalystSlots(int amount) {
table.addProperty("catalyst_slot", amount);
return this;
}

public ArtisJsonBuilder normalRecipes() {
table.addProperty("normal_recipes", true);
return this;
}

public ArtisJsonBuilder bypassCheck() {
table.addProperty("bypass_check", true);
return this;
}

public ArtisJsonBuilder copy(String original) {
settings.addProperty("copy", original);
return this;
}

public ArtisJsonBuilder material(String material) {
settings.addProperty("material", material);
return this;
}

public ArtisJsonBuilder materialColor(String material) {
settings.addProperty("material_color", material);
return this;
}

public ArtisJsonBuilder collidable(boolean value) {
settings.addProperty("collidable", value);
return this;
}

public ArtisJsonBuilder breakByHand(boolean value) {
settings.addProperty("break_by_hand", value);
return this;
}

// Currently no breakByTool, will add later

public ArtisJsonBuilder nonOpaque() {
settings.addProperty("non_opaque", true);
return this;
}

public ArtisJsonBuilder sounds(String sounds) {
settings.addProperty("sounds", sounds);
return this;
}

public ArtisJsonBuilder lightLevel(int level) {
settings.addProperty("light_level", level);
return this;
}

public ArtisJsonBuilder hardness(float hardness) {
settings.addProperty("hardness", hardness);
return this;
}

public ArtisJsonBuilder resistance(float resistance) {
settings.addProperty("resistance", resistance);
return this;
}

public ArtisJsonBuilder slipperiness(float slipperiness) {
settings.addProperty("slipperiness", slipperiness);
return this;
}

public ArtisJsonBuilder breakInstantly() {
settings.addProperty("break_instantly", true);
return this;
}

public ArtisJsonBuilder dropsNothing() {
settings.addProperty("drops_nothing", true);
return this;
}

public ArtisJsonBuilder dropsLike(String value) {
settings.addProperty("drops_like", value);
return this;
}

public ArtisJsonBuilder drops(String value) {
settings.addProperty("drops", value);
return this;
}

public ArtisJsonBuilder dynamicBounds() {
settings.addProperty("dynamic_bounds", true);
return this;
}

public void build() {
try {
ArtisData.loadData(ArtisData.jankson.load(root.toString()));
Artis.logger.info(root.toString());
} catch (SyntaxError syntaxError) {
Artis.logger.error("[Artis] Error converting JSON for KubeJS. Is JSON empty?");
}
}

public String getType() {
return "block";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.github.alloffabric.artis.compat.kubejs;

import dev.latvian.kubejs.event.EventJS;

/**
* @author LatvianModder
*/
public class ArtisJsonRegistryEventJS extends EventJS {
public ArtisJsonBuilder create(String name) {
ArtisJsonBuilder builder = new ArtisJsonBuilder(name);
return builder;
}

public ArtisJsonBuilder createExistingBlock(String modId, String name) {
ArtisJsonBuilder builder = new ArtisJsonBuilder(modId, name);
builder = builder.setType("existing_block");
return builder;
}

public ArtisJsonBuilder createExistingItem(String modId, String name) {
ArtisJsonBuilder builder = new ArtisJsonBuilder(modId, name);
builder = builder.setType("existing_item");
return builder;
}
}
Loading

0 comments on commit 8a03bbd

Please sign in to comment.