Skip to content

Commit

Permalink
Fix forge 0 division error
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Dec 4, 2021
1 parent b85f7f7 commit d74726c
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 120 deletions.
8 changes: 0 additions & 8 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ architectury {
common()
}

configurations {
dev
}

artifacts {
dev(jar)
}

tasks.withType(dokkaHtmlPartial.getClass()).configureEach {
pluginsMapConfiguration.set(
["org.jetbrains.dokka.base.DokkaBase": """{ "separateInheritedMembers": true}"""]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public class LoaderSpecific {
public static boolean permissions$checkPermission(CommandSourceStack source, String permissionNode, int fallbackLevel) {
throw new AssertionError();
}

@ExpectPlatform
public static int getItemRegistrySize() {
throw new AssertionError();
}
}
17 changes: 10 additions & 7 deletions common/src/main/java/org/samo_lego/taterzens/gui/EditorGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.concurrent.atomic.AtomicInteger;

import static org.samo_lego.taterzens.Taterzens.config;
import static org.samo_lego.taterzens.gui.MessagesEditGUI.getFromName;
import static org.samo_lego.taterzens.gui.ListItemsGUI.getFromName;

public class EditorGUI {

Expand Down Expand Up @@ -87,12 +87,15 @@ public static SimpleGui createCommandGui(ServerPlayer player, SimpleGui previous

for (int i = 1; i < examples.length; ++i) {
ItemStack exampleStack = new ItemStack(Items.PAPER);
exampleStack.setHoverName(new TranslatableComponent("options.autoSuggestCommands")
.append(": ")
.append(new TextComponent(examples[i]))
);

constructedGui.setSlot(i * 2 + 1, exampleStack); // 2 being the last slot index in anvil inventory
String example = examples[i];
exampleStack.setHoverName(new TextComponent(example));

// 2 being the last slot index in anvil inventory
constructedGui.setSlot(i * 2 + 1, new GuiElement(exampleStack, (index, type, action) -> {
String input = ((AnvilInputGui) constructedGui).getInput();
((AnvilInputGui) constructedGui).setDefaultInputValue(exampleStack.getHoverName().getString());
exampleStack.setHoverName(new TextComponent(input));
}));
}
} else {
// Creates the biggest possible container
Expand Down
17 changes: 17 additions & 0 deletions common/src/main/java/org/samo_lego/taterzens/gui/ListItemsGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.samo_lego.taterzens.compatibility.LoaderSpecific;

import static org.samo_lego.taterzens.Taterzens.config;

public abstract class ListItemsGUI extends SimpleGui implements Container {
protected static final CompoundTag customData = new CompoundTag();
private static final int REGISTRY_ITEMS_SIZE = LoaderSpecific.getItemRegistrySize();
private int currentPage = 0;

/**
Expand Down Expand Up @@ -79,6 +82,20 @@ public ListItemsGUI(ServerPlayer player, Component npcName, String titleTranslat
this.setSlot(8, closeScreenButton);
}

/**
* Gets an item from registry by string hash.
* @param name string to convert into item
* @return item, converted from string hash. If air would be returned, it is switched top stone instead.
*/
public static Item getFromName(String name) {
int i = Math.abs(name.hashCode());
Item item = Item.byId(i % REGISTRY_ITEMS_SIZE);
if (item.equals(Items.AIR))
item = Items.STONE;

return item;
}

/**
* Gets current page info (Page X of Y)
* @return translated page info text.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package org.samo_lego.taterzens.gui;

import com.mojang.datafixers.util.Pair;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.samo_lego.taterzens.mixin.accessors.MappedRegistryAccessor;
import org.samo_lego.taterzens.npc.TaterzenNPC;

import java.util.List;

import static org.samo_lego.taterzens.Taterzens.config;

public class MessagesEditGUI extends ListItemsGUI {
private static final int REGISTRY_ITEMS_SIZE = ((MappedRegistryAccessor) Registry.ITEM).getById().size();
private final List<Pair<Component, Integer>> messages;

/**
Expand Down Expand Up @@ -103,18 +98,4 @@ public int getMaxPages() {
return 0;
return this.messages.size() / this.getSize();
}

/**
* Gets an item from registry by string hash.
* @param name string to convert into item
* @return item, converted from string hash. If air would be returned, it is switched top stone instead.
*/
public static Item getFromName(String name) {
int i = Math.abs(name.hashCode());
Item item = Item.byId(i % REGISTRY_ITEMS_SIZE);
if (item.equals(Items.AIR))
item = Items.STONE;

return item;
}
}
1 change: 0 additions & 1 deletion common/src/main/resources/taterzens.common.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"accessors.CommandSourceStackAccessor",
"accessors.EntityAccessor",
"accessors.EntityTrackerEntryAccessor",
"accessors.MappedRegistryAccessor",
"accessors.PlayerAccessor"
],
"client": [],
Expand Down
8 changes: 2 additions & 6 deletions fabric/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,8 @@ dependencies {
//modImplementation("carpet:fabric-carpet:${rootProject.minecraft_version}-${project.carpet_core_version}")
modImplementation("carpet:fabric-carpet:1.18-pre4-${project.carpet_core_version}")

common(project(path: ":common", configuration: "dev")) {
transitive = false
}
shadowCommon(project(path: ":common", configuration: "transformProductionFabric")) {
transitive = false
}
common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionFabric")) { transitive false }
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package org.samo_lego.taterzens.compatibility.fabric;

import net.minecraft.commands.CommandSourceStack;
import org.samo_lego.taterzens.fabric.mixin.MappedRegistryAccessor;

import static net.minecraft.core.Registry.ITEM;
import static org.samo_lego.taterzens.Taterzens.LUCKPERMS_LOADED;
import static org.samo_lego.taterzens.compatibility.PermissionHelper.checkPermission;

import net.minecraft.commands.CommandSourceStack;

public class LoaderSpecificImpl {

private static final int REGISTRY_ITEMS_SIZE = ((MappedRegistryAccessor) ITEM).getById().size();

public static boolean permissions$checkPermission(CommandSourceStack source, String permissionNode, int fallbackLevel) {
return LUCKPERMS_LOADED ? checkPermission(source, permissionNode, fallbackLevel) : source.hasPermission(fallbackLevel);
}

public static int getItemRegistrySize() {
return REGISTRY_ITEMS_SIZE;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.samo_lego.taterzens.mixin.accessors;
package org.samo_lego.taterzens.fabric.mixin;

import it.unimi.dsi.fastutil.objects.ObjectList;
import net.minecraft.core.MappedRegistry;
Expand Down
3 changes: 2 additions & 1 deletion fabric/src/main/resources/taterzens.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"package": "org.samo_lego.taterzens.fabric.mixin",
"compatibilityLevel": "JAVA_16",
"mixins": [
"RegistrySyncManagerMixin_TaterzenSyncDisabler",
"MappedRegistryAccessor",
"RegistrySyncManagerMixin_TaterzenSyncDisabler"
],
"client": [],
"injectors": {
Expand Down
20 changes: 6 additions & 14 deletions forge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,16 @@ dependencies {
forge "net.minecraftforge:forge:${rootProject.minecraft_version}-${rootProject.forge_version}"

// Libs
modImplementation "com.github.samolego.Config2Brigadier:config2brigadier-forge:${rootProject.c2b_version}"
shadowCommon("com.github.samolego.Config2Brigadier:config2brigadier-forge:${rootProject.c2b_version}") {
transitive = false
}
modApi("com.github.samolego.Config2Brigadier:config2brigadier-forge:${rootProject.c2b_version}")
shadowCommon("com.github.samolego.Config2Brigadier:config2brigadier-forge:${rootProject.c2b_version}") { transitive false }

// SGUI
modImplementation "com.github.samolego:forgified-sgui:${project.fsgui_version}"
shadowCommon("com.github.samolego:forgified-sgui:${project.fsgui_version}") {
transitive = false
}
modApi("com.github.samolego:forgified-sgui:${project.fsgui_version}")
shadowCommon("com.github.samolego:forgified-sgui:${project.fsgui_version}") { transitive false}


common(project(path: ":common", configuration: "dev")) {
transitive = false
}
shadowCommon(project(path: ":common", configuration: "transformProductionForge")) {
transitive = false
}
common(project(path: ":common", configuration: "namedElements")) { transitive false }
shadowCommon(project(path: ":common", configuration: "transformProductionForge")) { transitive false }
}

processResources {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package org.samo_lego.taterzens.compatibility.forge;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.registries.RegistryManager;

public class LoaderSpecificImpl {

private static final ResourceLocation ITEM_ID = new ResourceLocation("item");

public static boolean permissions$checkPermission(CommandSourceStack source, String permissionNode, int fallbackLevel) {
return source.hasPermission(fallbackLevel);
}

public static int getItemRegistrySize() {
return RegistryManager.ACTIVE.getRegistry(ITEM_ID).getValues().size();
}
}
63 changes: 3 additions & 60 deletions forge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,13 @@ logoFile = "taterzens_icon.png"
[[dependencies.taterzens]]
modId = "forge"
mandatory = true
versionRange = "[37,)"
versionRange = "[38,)"
ordering = "NONE"
side = "SERVER"

[[dependencies.taterzens]]
modId = "minecraft"
mandatory = true
versionRange = "[1.17.1,)"
versionRange = "[1.18,)"
ordering = "NONE"
side = "SERVER"

# C2B Lib
modLoader = "javafml"
loaderVersion = "[37,)"
license = "LGPL-v3.0"

[[mods]]
modId = "config2brigadier"
version = "${c2b_version}"
displayName = "Config to Brigadier"
authors = "samo_lego"
description = '''
A library to automagically generate in-game command for config editing.
'''

[[dependencies.config2brigadier]]
modId = "forge"
mandatory = true
versionRange = "[37,)"
ordering = "NONE"
side = "BOTH"

[[dependencies.config2brigadier]]
modId = "minecraft"
mandatory = true
versionRange = "[1.17.1,)"
ordering = "NONE"
side = "BOTH"

# Forgified SGUI
modLoader = "javafml"
loaderVersion = "[35,)"
license = "LGPL-v3.0"

[[mods]]
modId = "sgui"
version = "${sgui_version}"
displayName = "Server GUI lib"
authors = "patbox"
contributors="aws404, samo_lego"
description = '''
Library for creating server side guis!
'''

[[dependencies.sgui]]
modId = "forge"
mandatory = true
versionRange = "[35,)"
ordering = "NONE"
side = "BOTH"

[[dependencies.sgui]]
modId = "minecraft"
mandatory = true
versionRange = "[1.17.1,)"
ordering = "NONE"
side = "BOTH"
side = "SERVER"
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fabric_version=0.43.1+1.18
forge_version=38.0.10

# Mod Properties
mod_version = 1.6.3
mod_version = 1.6.4
maven_group = org.samo_lego
archives_base_name = taterzens

Expand Down

0 comments on commit d74726c

Please sign in to comment.