diff --git a/build.gradle b/build.gradle index 62d4270..04b4bab 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { id 'net.minecraftforge.gradle' version '5.1.+' } -version = '1.19-1.0.2' +version = '1.19-1.0.3' group = 'com.laosun.stackone' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'stackone' diff --git a/src/main/java/com/laosun/stackone/IgnoreItem.java b/src/main/java/com/laosun/stackone/IgnoreItem.java new file mode 100644 index 0000000..2f587d7 --- /dev/null +++ b/src/main/java/com/laosun/stackone/IgnoreItem.java @@ -0,0 +1,5 @@ +package com.laosun.stackone; + +public class IgnoreItem { + public String item; +} diff --git a/src/main/java/com/laosun/stackone/StackOneMod.java b/src/main/java/com/laosun/stackone/StackOneMod.java index f756e31..0fee759 100644 --- a/src/main/java/com/laosun/stackone/StackOneMod.java +++ b/src/main/java/com/laosun/stackone/StackOneMod.java @@ -1,51 +1,92 @@ package com.laosun.stackone; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; import com.mojang.logging.LogUtils; -import net.minecraft.client.Minecraft; -import net.minecraft.world.item.BlockItem; -import net.minecraft.world.item.CreativeModeTab; import net.minecraft.world.item.Item; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.state.BlockBehaviour; -import net.minecraft.world.level.material.Material; -import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.IEventBus; -import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; -import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; -import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; -import net.minecraftforge.registries.RegistryObject; import org.slf4j.Logger; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; import java.lang.reflect.Field; +import java.util.ArrayList; -// The value here should match an entry in the META-INF/mods.toml file @Mod(StackOneMod.MODID) -public class StackOneMod -{ +public class StackOneMod { // Define mod id in a common place for everything to reference public static final String MODID = "stackone"; // Directly reference a slf4j logger private static final Logger LOGGER = LogUtils.getLogger(); // Create a Deferred Register to hold Blocks which will all be registered under the "examplemod" namespace - public StackOneMod() - { + public StackOneMod() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); // Register the commonSetup method for modloading modEventBus.addListener(this::commonSetup); } - private void commonSetup(final FMLCommonSetupEvent event) - { - for(Item i : ForgeRegistries.ITEMS){ + private void commonSetup(final FMLCommonSetupEvent event) { + File dir = new File("config/stackone"); + if (!dir.exists()) { + dir.mkdirs(); + } + File file = new File(dir, "ignore_item.json"); + if (!file.isFile()) { + try { + if (!file.createNewFile()) { + LOGGER.error("Fail to create file!"); + } + } catch (IOException e) { + LOGGER.error("Fail to create file!"); + e.printStackTrace(); + } + } + char[] a1 = null; + try { + FileReader fileReader = new FileReader(file); + a1 = new char[(int) file.length()]; + fileReader.read(a1); + } catch (IOException e) { + LOGGER.error("Fail to open file!"); + e.printStackTrace(); + } + Gson gson = new Gson(); + JsonArray jsonArray; + ArrayList ignoreItems = new ArrayList<>(); + try { + if (a1 != null) { + jsonArray = JsonParser.parseString(String.copyValueOf(a1)).getAsJsonArray(); + for (JsonElement user : jsonArray) { + IgnoreItem ignore = gson.fromJson(user, IgnoreItem.class); + ignoreItems.add(ignore.item); + } + } + + } catch (Exception e) { + LOGGER.error("Json has syntax error!"); + e.printStackTrace(); + } + for (Item i : ForgeRegistries.ITEMS) { Field a; + boolean is = false; + for (String j : ignoreItems) { + if (j.equals(i.getDescriptionId())) { + is = true; + break; + } + } + if (is) { + continue; + } try { a = Item.class.getDeclaredField("maxStackSize"); } catch (NoSuchFieldException e) { @@ -64,6 +105,4 @@ private void commonSetup(final FMLCommonSetupEvent event) } } } - - } diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 804c6b5..16f90d2 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -15,21 +15,21 @@ license="All rights reserved" # A list of mods - how many allowed here is determined by the individual mod loader [[mods]] #mandatory # The modid of the mod -modId="stackone" #mandatory +modId = "stackone" #mandatory # The version number of the mod - there's a few well known ${} variables useable here or just hardcode it # ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata # see the associated build.gradle script for how to populate this completely automatically during a build -version="1.19-1.0.2" #mandatory - # A display name for the mod -displayName="StackOne Mod" #mandatory +version = "1.19-1.0.3" #mandatory +# A display name for the mod +displayName = "StackOne Mod" #mandatory # A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/ #updateJSONURL="https://change.me.example.invalid/updates.json" #optional # A URL for the "homepage" for this mod, displayed in the mod UI #displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional # A file name (in the root of the mod JAR) containing a logo for display -logoFile="logo.png" #optional +logoFile = "logo.png" #optional # A text field displayed in the mod UI -authors="Lao Sun" #optional +authors = "Lao Sun" #optional # Display Test controls the display for your mod in the server connection screen # MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod. # IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod.