Skip to content

Commit

Permalink
1.0.3 1
Browse files Browse the repository at this point in the history
  • Loading branch information
12345-mcpython committed Jul 15, 2022
1 parent ca0952e commit 8d8509f
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 54 deletions.
56 changes: 56 additions & 0 deletions src/main/java/com/laosun/stackone/IgnoreItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
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 java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import static com.laosun.stackone.StackOneMod.LOGGER;

public class IgnoreItem {
public String item;

public static ArrayList<String> getIgnoreItems() {
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<String> 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();
}
return ignoreItems;
}
}
63 changes: 63 additions & 0 deletions src/main/java/com/laosun/stackone/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.laosun.stackone.StackOneMod.LOGGER;

public class Stack {
public String item;
public short max_stack_size;

public static Map<String, Short> getStackList() {
Map<String, Short> map = new HashMap<>();
File dir = new File("config/stackone");
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, "max_stack_size.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;
try {
if (a1 != null) {
jsonArray = JsonParser.parseString(String.copyValueOf(a1)).getAsJsonArray();
for (JsonElement user : jsonArray) {
Stack ignore = gson.fromJson(user, Stack.class);
map.put(ignore.item, ignore.max_stack_size);
}
}

} catch (Exception e) {
LOGGER.error("Json has syntax error!");
e.printStackTrace();
}
return map;
}
}
68 changes: 14 additions & 54 deletions src/main/java/com/laosun/stackone/StackOneMod.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
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.world.item.Item;
import net.minecraftforge.eventbus.api.IEventBus;
Expand All @@ -13,9 +9,6 @@
import net.minecraftforge.registries.ForgeRegistries;
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;

Expand All @@ -24,7 +17,7 @@ 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();
public static final Logger LOGGER = LogUtils.getLogger();
// Create a Deferred Register to hold Blocks which will all be registered under the "examplemod" namespace

public StackOneMod() {
Expand All @@ -34,57 +27,23 @@ public StackOneMod() {
modEventBus.addListener(this::commonSetup);
}

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();
public boolean isItem(ArrayList<String> descriptionId, Item item) {
boolean is = false;
for (String i : descriptionId) {
if (i.equals(item.getDescriptionId())) {
is = true;
break;
}
}
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<String> 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);
}
}
return is;
}

} catch (Exception e) {
LOGGER.error("Json has syntax error!");
e.printStackTrace();
}

private void commonSetup(final FMLCommonSetupEvent event) {
ArrayList<String> ignoreItems = IgnoreItem.getIgnoreItems();
for (Item i : ForgeRegistries.ITEMS) {
Field a;
boolean is = false;
for (String j : ignoreItems) {
if (j.equals(i.getDescriptionId())) {
is = true;
break;
}
}
if (is) {
if (isItem(ignoreItems, i)) {
continue;
}
try {
Expand All @@ -99,6 +58,7 @@ private void commonSetup(final FMLCommonSetupEvent event) {
}
a.setAccessible(true);
try {

a.set(i, 1);
} catch (IllegalAccessException e) {
LOGGER.error("Failed to set field!!!");
Expand Down

0 comments on commit 8d8509f

Please sign in to comment.