Skip to content

Commit a63838e

Browse files
committed
修复一个特殊环境下会出现的bug
可能优化了性能
1 parent 6b3aa77 commit a63838e

20 files changed

Lines changed: 359 additions & 249 deletions

.github/workflows/deploy.yml

Lines changed: 0 additions & 61 deletions
This file was deleted.

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010
id 'java-library'
1111
id 'maven-publish'
1212
id 'org.jetbrains.gradle.plugin.idea-ext' version '1.1.9'
13-
id 'com.gtnewhorizons.retrofuturagradle' version '1.4.1'
13+
id 'com.gtnewhorizons.retrofuturagradle' version '2.0.2'
1414
id 'org.jetbrains.changelog' version '2.2.1'
1515
}
1616

gradle.properties

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ show_testing_output = false
1515

1616
# Mod Information
1717
# HIGHLY RECOMMEND complying with SemVer for mod_version: https://semver.org/
18-
mod_version = 1.2.4
18+
mod_version = 1.2.5
1919
root_package = com.circulation
2020
mod_id = only_one_item
2121
mod_name = OnlyOneItem
@@ -109,6 +109,7 @@ mixin_configs = ${mod_id}
109109
# A refmap is a json that denotes mapping conversions, this json is generated automatically, with the name `mixins.mod_id.refmap.json`
110110
# Use the property `mixin_refmap` if you want it to use a different name, only one name is accepted
111111
mixin_refmap = mixins.${mod_id}.refmap.json
112+
mixin_package = com.circulation.only_one_item.mixin.mc
112113

113114
# Coremods
114115
# The most powerful way to change java classes at runtime, it is however very primitive with little documentation.
@@ -123,4 +124,4 @@ coremod_plugin_class_name = com.circulation.only_one_item.mixin.EarlyMixinLoader
123124
# Convenient way to allow downloading of assets from official vanilla Minecraft servers, CurseForge, or any direct links
124125
# Documentation: https://github.com/CleanroomMC/AssetMover
125126
use_asset_mover = false
126-
asset_mover_version = 2.5
127+
asset_mover_version = 2.5

gradle/scripts/publishing.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if (propertyBool('publish_to_curseforge')) {
4747
addGameVersion 'Forge'
4848
addGameVersion '1.12.2'
4949
releaseType = propertyString('release_type')
50-
if (!propertyBool('publish_with_changelog')) {
50+
if (propertyBool('publish_with_changelog')) {
5151
changelog = parserChangelog()
5252
changelogType = 'markdown'
5353
}
@@ -78,7 +78,7 @@ if (propertyBool('publish_to_modrinth')) {
7878
assertProperty('release_type')
7979
setDefaultProperty('modrinth_debug', false, false)
8080
modrinth {
81-
token = System.getenv('MODRINTH_TOKEN') ? "" : System.getenv('MODRINTH_TOKEN')
81+
token = System.getenv('MODRINTH_TOKEN') ?: ""
8282
projectId = propertyString('modrinth_project_id')
8383
versionNumber = propertyString('mod_version')
8484
versionType = propertyString('release_type')
@@ -96,12 +96,12 @@ if (propertyBool('publish_to_modrinth')) {
9696
}
9797
}
9898
}
99-
if (!propertyBool('publish_with_changelog')) {
99+
if (propertyBool('publish_with_changelog')) {
100100
changelog = parserChangelog()
101101
}
102102
if (propertyBool('modrinth_sync_readme')) {
103103
syncBodyFrom = file('README.md').text
104104
tasks.modrinth.dependsOn(tasks.modrinthSyncBody)
105105
}
106106
}
107-
}
107+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.4.1-bin.zip
44
networkTimeout=10000
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

src/main/java/com/circulation/only_one_item/OOIConfig.java

Lines changed: 103 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import com.circulation.only_one_item.handler.MatchFluidHandler;
77
import com.circulation.only_one_item.handler.MatchItemHandler;
88
import com.circulation.only_one_item.util.BlackMatchItem;
9+
import com.circulation.only_one_item.util.MatchItem;
910
import com.google.gson.GsonBuilder;
1011
import com.google.gson.reflect.TypeToken;
1112
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
1213
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
14+
import net.minecraft.item.Item;
15+
import net.minecraft.util.ResourceLocation;
1316
import net.minecraftforge.fluids.FluidRegistry;
1417
import net.minecraftforge.fml.common.Loader;
1518

@@ -26,19 +29,23 @@ public class OOIConfig {
2629
public static final List<FluidConversionTarget> fluids = new ObjectArrayList<>();
2730
public static final Set<BlackMatchItem> blackList = new ObjectOpenHashSet<>();
2831

29-
public static void readConfig() throws IOException {
32+
public static void readConfig() {
3033
var configPath = Loader.instance().getConfigDir().toPath().resolve("ooi");
3134
var ooiPath = configPath.resolve("ooi_item.json");
3235
var blackPath = configPath.resolve("ooi_item_black_list.json");
3336
var ooiFluidPath = configPath.resolve("ooi_fluid.json");
3437

35-
Files.createDirectories(configPath);
38+
try {
39+
Files.createDirectories(configPath);
40+
} catch (IOException e) {
41+
throw new RuntimeException(e);
42+
}
3643

3744
var config = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
3845

3946
if (Files.exists(ooiPath)) {
4047
try {
41-
items.addAll(config.fromJson(new String(Files.readAllBytes(ooiPath), StandardCharsets.UTF_8), (new TypeToken<Set<ItemConversionTarget>>() {
48+
addItemTargets(config.fromJson(new String(Files.readAllBytes(ooiPath), StandardCharsets.UTF_8), (new TypeToken<Set<ItemConversionTarget>>() {
4249
}).getType()));
4350
} catch (Exception ignored) {
4451
OnlyOneItem.LOGGER.error("[OOI]The config/ooi/ooi_item.json file is incorrect!");
@@ -50,24 +57,30 @@ public static void readConfig() throws IOException {
5057
if (inputStream != null) {
5158
Files.copy(inputStream, ooiPath);
5259
}
60+
} catch (IOException e) {
61+
throw new RuntimeException(e);
5362
}
5463
}
5564

5665
if (Files.exists(ooiFluidPath)) {
5766
try {
58-
fluids.addAll(config.fromJson(new String(Files.readAllBytes(ooiFluidPath), StandardCharsets.UTF_8), (new TypeToken<Set<FluidConversionTarget>>() {
67+
addFluidTargets(config.fromJson(new String(Files.readAllBytes(ooiFluidPath), StandardCharsets.UTF_8), (new TypeToken<Set<FluidConversionTarget>>() {
5968
}).getType()));
6069
} catch (Exception ignored) {
6170
OnlyOneItem.LOGGER.error("[OOI]The config/ooi/ooi_fluid.json file is incorrect!");
6271
}
6372
} else {
6473
fluids.add(new FluidConversionTarget(FluidRegistry.WATER.getName()).addMatchFluid(FluidRegistry.WATER.getName()));
65-
Files.write(ooiFluidPath, config.toJson(fluids).getBytes(StandardCharsets.UTF_8));
74+
try {
75+
Files.write(ooiFluidPath, config.toJson(fluids).getBytes(StandardCharsets.UTF_8));
76+
} catch (IOException e) {
77+
throw new RuntimeException(e);
78+
}
6679
}
6780

6881
if (Files.exists(blackPath)) {
6982
try {
70-
blackList.addAll(config.fromJson(new String(Files.readAllBytes(blackPath), StandardCharsets.UTF_8), (new TypeToken<Set<BlackMatchItem>>() {
83+
addBlackList(config.fromJson(new String(Files.readAllBytes(blackPath), StandardCharsets.UTF_8), (new TypeToken<Set<BlackMatchItem>>() {
7184
}).getType()));
7285
} catch (Exception ignored) {
7386
OnlyOneItem.LOGGER.error("[OOI]The config/ooi/ooi_item_black_list.json file is incorrect!");
@@ -76,9 +89,91 @@ public static void readConfig() throws IOException {
7689
blackList.add(BlackMatchItem.getInstance("minecraft:gold_ingot", 0));
7790
blackList.add(BlackMatchItem.getInstance(Type.OreDict, "ingotGold"));
7891
blackList.add(BlackMatchItem.getInstance(Type.ModID, "minecraft"));
79-
Files.write(blackPath, config.toJson(blackList).getBytes(StandardCharsets.UTF_8));
92+
try {
93+
Files.write(blackPath, config.toJson(blackList).getBytes(StandardCharsets.UTF_8));
94+
} catch (IOException e) {
95+
throw new RuntimeException(e);
96+
}
8097
}
8198
MatchItemHandler.InitTarget();
8299
MatchFluidHandler.Init(OOIConfig.fluids);
83100
}
84-
}
101+
102+
private static void addItemTargets(Set<ItemConversionTarget> targets) {
103+
if (targets == null) return;
104+
for (ItemConversionTarget target : targets) {
105+
if (target == null || !isRegisteredItem(target.getTargetID()) || target.getMatchItems() == null) {
106+
continue;
107+
}
108+
Set<MatchItem> validMatches = new ObjectOpenHashSet<>();
109+
for (MatchItem matchItem : target.getMatchItems()) {
110+
if (isValidMatchItem(matchItem)) {
111+
validMatches.add(matchItem);
112+
}
113+
}
114+
if (!validMatches.isEmpty()) {
115+
items.add(target.setMatchItem(validMatches));
116+
}
117+
}
118+
}
119+
120+
private static void addFluidTargets(Set<FluidConversionTarget> targets) {
121+
if (targets == null) return;
122+
for (FluidConversionTarget target : targets) {
123+
if (target == null || target.getTarget() == null || target.getMatchFluids() == null) {
124+
continue;
125+
}
126+
Set<String> validMatches = new ObjectOpenHashSet<>();
127+
for (String fluid : target.getMatchFluids()) {
128+
if (isNotBlank(fluid)) {
129+
validMatches.add(fluid);
130+
}
131+
}
132+
if (!validMatches.isEmpty()) {
133+
fluids.add(target.setMatchFluids(validMatches));
134+
}
135+
}
136+
}
137+
138+
private static void addBlackList(Set<BlackMatchItem> targets) {
139+
if (targets == null) return;
140+
for (BlackMatchItem target : targets) {
141+
if (isValidBlackList(target)) {
142+
blackList.add(target);
143+
}
144+
}
145+
}
146+
147+
private static boolean isValidMatchItem(MatchItem matchItem) {
148+
if (matchItem == null) return false;
149+
if (isNotBlank(matchItem.oreName())) return true;
150+
return isValidResourceLocation(matchItem.id());
151+
}
152+
153+
private static boolean isValidBlackList(BlackMatchItem target) {
154+
if (target == null || target.type() == null || !isNotBlank(target.name())) return false;
155+
return switch (target.type()) {
156+
case Item -> isValidResourceLocation(target.name());
157+
case OreDict, ModID -> true;
158+
};
159+
}
160+
161+
private static boolean isRegisteredItem(String id) {
162+
if (!isValidResourceLocation(id)) return false;
163+
return Item.getByNameOrId(id) != null;
164+
}
165+
166+
private static boolean isValidResourceLocation(String id) {
167+
if (!isNotBlank(id)) return false;
168+
try {
169+
new ResourceLocation(id);
170+
return true;
171+
} catch (RuntimeException ignored) {
172+
return false;
173+
}
174+
}
175+
176+
private static boolean isNotBlank(String value) {
177+
return value != null && !value.trim().isEmpty();
178+
}
179+
}

src/main/java/com/circulation/only_one_item/OnlyOneItem.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import net.minecraftforge.common.MinecraftForge;
77
import net.minecraftforge.fml.common.Loader;
88
import net.minecraftforge.fml.common.Mod;
9-
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
109
import net.minecraftforge.fml.common.event.FMLLoadCompleteEvent;
1110
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
1211
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
@@ -15,8 +14,6 @@
1514
import org.apache.logging.log4j.LogManager;
1615
import org.apache.logging.log4j.Logger;
1716

18-
import java.io.IOException;
19-
2017
@Mod(modid = OnlyOneItem.MOD_ID, name = Tags.MOD_NAME, version = Tags.VERSION,
2118
dependencies = "required-after:mixinbooter@[8.0,);"
2219
)
@@ -33,22 +30,12 @@ public class OnlyOneItem {
3330
@Mod.EventHandler
3431
public void preInit(FMLPreInitializationEvent event) {
3532
MinecraftForge.EVENT_BUS.register(new OreDictionaryHandler());
36-
try {
37-
OOIConfig.readConfig();
38-
} catch (IOException ignored) {
39-
40-
}
41-
}
42-
43-
@Mod.EventHandler
44-
public void init(FMLInitializationEvent event) {
45-
if (!Loader.isModLoaded("crafttweaker")) {
46-
InitHandler.allPreInit();
47-
}
33+
OOIConfig.readConfig();
4834
}
4935

5036
@Mod.EventHandler
5137
public void postInit(FMLPostInitializationEvent event) {
38+
InitHandler.allPreInit();
5239
if (!Loader.isModLoaded("unidict")) {
5340
MatchItemHandler.clearRecipe();
5441
}
@@ -59,4 +46,4 @@ public void loadComplete(FMLLoadCompleteEvent event) {
5946
MatchItemHandler.postODProcess();
6047
}
6148

62-
}
49+
}

src/main/java/com/circulation/only_one_item/handler/InitHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
public class InitHandler {
99

1010
public static void allPreInit() {
11+
MatchFluidHandler.lock();
1112
MatchItemHandler.preItemStackInit();
1213
MatchFluidHandler.preFluidStackInit();
1314

0 commit comments

Comments
 (0)