Skip to content

Commit e669b4b

Browse files
committed
start of work
1 parent f7ad0bc commit e669b4b

File tree

10 files changed

+118
-22
lines changed

10 files changed

+118
-22
lines changed

src/main/java/gregtech/api/items/toolitem/IGTTool.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ default Material getToolMaterial(ItemStack stack) {
247247
String string = toolTag.getString(MATERIAL_KEY);
248248
Material material = GregTechAPI.materialManager.getMaterial(string);
249249
if (material == null) {
250-
toolTag.setString(MATERIAL_KEY, (material = Materials.Iron).toString());
250+
toolTag.setString(MATERIAL_KEY, (material = Materials.Iron).getRegistryName());
251251
}
252252
return material;
253253
}

src/main/java/gregtech/api/items/toolitem/ToolHelper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public static ItemStack getAndSetToolData(IGTTool tool, Material material, int m
196196
ItemStack stack = tool.getRaw();
197197
GTUtility.getOrCreateNbtCompound(stack).setInteger(HIDE_FLAGS, 2);
198198
NBTTagCompound toolTag = getToolTag(stack);
199-
toolTag.setString(MATERIAL_KEY, material.toString());
199+
toolTag.setString(MATERIAL_KEY, material.getRegistryName());
200200
toolTag.setInteger(MAX_DURABILITY_KEY, maxDurability);
201201
toolTag.setInteger(HARVEST_LEVEL_KEY, harvestLevel);
202202
toolTag.setFloat(TOOL_SPEED_KEY, toolSpeed);

src/main/java/gregtech/api/unification/material/registry/IMaterialRegistryManager.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ public interface IMaterialRegistryManager {
1818
@NotNull
1919
MaterialRegistry createRegistry(@NotNull String modid);
2020

21+
/**
22+
* Check if a mod's registry exists. Accessible during all phases.
23+
*
24+
* @param modid the modid of the registry
25+
* @return if the registry exists
26+
*/
27+
boolean hasRegistry(@NotNull String modid);
28+
2129
/**
2230
* Get a mod's registry. Accessible during all phases.
2331
*
2432
* @param modid the modid of the mod
25-
* @return the registry associated with the mod, or the GregTech registry if it does not have one
33+
* @return the registry associated with the mod
2634
*/
2735
@NotNull
2836
MaterialRegistry getRegistry(@NotNull String modid);
@@ -31,7 +39,7 @@ public interface IMaterialRegistryManager {
3139
* Get a mod's registry. Accessible during all phases.
3240
*
3341
* @param networkId the network ID of the registry
34-
* @return the registry associated with the network ID, or the GregTech registry if it does not have one
42+
* @return the registry associated with the network ID
3543
*/
3644
@NotNull
3745
MaterialRegistry getRegistry(int networkId);

src/main/java/gregtech/core/unification/material/internal/MaterialRegistryManager.java

+26-7
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,31 @@ public MaterialRegistry createRegistry(@NotNull String modid) {
5555
return registry;
5656
}
5757

58+
@Override
59+
public boolean hasRegistry(@NotNull String modid) {
60+
return registries.containsKey(modid);
61+
}
62+
5863
@NotNull
5964
@Override
6065
public MaterialRegistry getRegistry(@NotNull String modid) {
6166
MaterialRegistry registry = registries.get(modid);
62-
return registry != null ? registry : gregtechRegistry;
67+
if (registry == null) {
68+
throw new IllegalArgumentException("No Material Registry found for modid: " + modid);
69+
}
70+
71+
return registry;
6372
}
6473

6574
@NotNull
6675
@Override
6776
public MaterialRegistry getRegistry(int networkId) {
6877
MaterialRegistry registry = networkIds.get(networkId);
69-
return registry != null ? registry : gregtechRegistry;
78+
if (registry == null) {
79+
throw new IllegalArgumentException("No Material Registry found for network id: " + networkId);
80+
}
81+
82+
return registry;
7083
}
7184

7285
@NotNull
@@ -92,17 +105,23 @@ public Collection<Material> getRegisteredMaterials() {
92105
@Override
93106
public Material getMaterial(@NotNull String name) {
94107
if (!name.isEmpty()) {
95-
String modid;
108+
MaterialRegistry registry;
96109
String materialName;
97110
int index = name.indexOf(':');
98111
if (index >= 0) {
99-
modid = name.substring(0, index);
100-
materialName = name.substring(index + 1);
112+
String modid = name.substring(0, index);
113+
if (hasRegistry(modid)) {
114+
registry = getRegistry(modid);
115+
materialName = name.substring(index + 1);
116+
} else {
117+
return null;
118+
}
101119
} else {
102-
modid = GTValues.MODID;
120+
registry = gregtechRegistry;
103121
materialName = name;
104122
}
105-
return getRegistry(modid).getObject(materialName);
123+
124+
return registry.getObject(materialName);
106125
}
107126
return null;
108127
}

src/main/java/gregtech/datafix/GTDataFixers.java

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import gregtech.api.metatileentity.registry.MTERegistry;
66
import gregtech.datafix.migration.impl.MigrateMTEBlockTE;
77
import gregtech.datafix.migration.impl.MigrateMTEItems;
8+
import gregtech.datafix.migration.impl.MigrateMaterialBlock;
89
import gregtech.datafix.migration.lib.MTERegistriesMigrator;
910
import gregtech.datafix.walker.WalkItemStackLike;
1011

@@ -58,6 +59,10 @@ private static void registerFixes(@NotNull GTDataVersion version, @NotNull ModFi
5859
fixer.registerFix(GTFixType.ITEM_STACK_LIKE, new MigrateMTEItems(migrator));
5960
fixer.registerFix(FixTypes.CHUNK, new MigrateMTEBlockTE(migrator));
6061
}
62+
case V2_POST_MATERIALS -> {
63+
// fixer.registerFix(GTFixType.ITEM_STACK_LIKE, new MigrateMaterialBlock());
64+
fixer.registerFix(FixTypes.CHUNK, new MigrateMaterialBlock());
65+
}
6166
default -> {}
6267
}
6368
}

src/main/java/gregtech/datafix/GTDataVersion.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ public enum GTDataVersion {
1414
/**
1515
* Version of data after multiple MTE registries were possible
1616
*/
17-
V1_POST_MTE;
17+
V1_POST_MTE,
18+
/**
19+
* Version of data after independent material registries were required
20+
*/
21+
V2_POST_MATERIALS
22+
;
1823

1924
static final @NotNull GTDataVersion @NotNull [] VALUES = values();
2025

src/main/java/gregtech/datafix/migration/impl/MigrateMTEBlockTE.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ public class MigrateMTEBlockTE implements IFixableData {
3030
private static final String X = "x";
3131
private static final String Y = "y";
3232
private static final String Z = "z";
33-
private static final String X_POS = "xPos";
34-
private static final String Z_POS = "zPos";
35-
private static final String CHUNK_SECTION_Y = "Y";
36-
private static final String CHUNK_SECTION_BLOCKS = "Blocks";
37-
private static final String CHUNK_SECTION_DATA = "Data";
38-
private static final String CHUNK_SECTION_ADD = "Add";
39-
40-
private static final int BLOCKS_PER_SECTION = 4096;
4133

4234
private final MTEMigrator migrator;
4335

@@ -101,7 +93,7 @@ private static void processChunkSections(@NotNull NBTTagCompound level,
10193
}
10294

10395
var blockStateIDMap = GameData.getBlockStateIDMap();
104-
ChunkPos chunkPos = new ChunkPos(level.getInteger(X_POS), level.getInteger(Z_POS));
96+
ChunkPos chunkPos = new ChunkPos(level.getInteger(LEVEL_CHUNK_X_POS), level.getInteger(LEVEL_CHUNK_Z_POS));
10597
NBTTagList sectionTagList = level.getTagList(SECTIONS, Constants.NBT.TAG_COMPOUND);
10698
for (int i = 0; i < sectionTagList.tagCount(); i++) {
10799
NBTTagCompound chunkSectionTag = sectionTagList.getCompoundTagAt(i);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package gregtech.datafix.migration.impl;
2+
3+
import net.minecraft.block.Block;
4+
import net.minecraft.nbt.NBTTagCompound;
5+
import net.minecraft.nbt.NBTTagList;
6+
import net.minecraft.util.datafix.IFixableData;
7+
8+
import net.minecraft.util.math.BlockPos;
9+
import net.minecraft.util.math.ChunkPos;
10+
import net.minecraft.world.chunk.NibbleArray;
11+
import net.minecraftforge.common.util.Constants;
12+
import net.minecraftforge.fml.common.registry.ForgeRegistries;
13+
import net.minecraftforge.registries.ForgeRegistry;
14+
import net.minecraftforge.registries.GameData;
15+
16+
import org.jetbrains.annotations.NotNull;
17+
18+
import static gregtech.datafix.util.DataFixConstants.*;
19+
20+
public class MigrateMaterialBlock implements IFixableData {
21+
22+
@Override
23+
public int getFixVersion() {
24+
return 2;
25+
}
26+
27+
@Override
28+
public @NotNull NBTTagCompound fixTagCompound(@NotNull NBTTagCompound compound) {
29+
var blockStateIDMap = GameData.getBlockStateIDMap();
30+
var r = ((ForgeRegistry<Block>) ForgeRegistries.BLOCKS);
31+
NBTTagCompound level = compound.getCompoundTag(LEVEL_TAG);
32+
ChunkPos chunkPos = new ChunkPos(level.getInteger(LEVEL_CHUNK_X_POS), level.getInteger(LEVEL_CHUNK_Z_POS));
33+
NBTTagList sectionTagList = level.getTagList(SECTIONS, Constants.NBT.TAG_COMPOUND);
34+
for (int i = 0; i < sectionTagList.tagCount(); i++) {
35+
NBTTagCompound chunkSectionTag = sectionTagList.getCompoundTagAt(i);
36+
37+
int sectionY = chunkSectionTag.getByte(CHUNK_SECTION_Y);
38+
byte[] blockIDs = chunkSectionTag.getByteArray(CHUNK_SECTION_BLOCKS);
39+
NibbleArray blockData = new NibbleArray(chunkSectionTag.getByteArray(CHUNK_SECTION_DATA));
40+
NibbleArray extendedIDs = chunkSectionTag.hasKey(CHUNK_SECTION_ADD, Constants.NBT.TAG_BYTE_ARRAY) ?
41+
new NibbleArray(chunkSectionTag.getByteArray(CHUNK_SECTION_ADD)) : null;
42+
for (int j = 0; j < BLOCKS_PER_SECTION; j++) {
43+
int x = j & 0x0F;
44+
int y = j >> 8 & 0x0F;
45+
int z = j >> 4 & 0x0F;
46+
47+
BlockPos pos = chunkPos.getBlock(x, sectionY << 4 | y, z);
48+
49+
}
50+
}
51+
52+
return compound;
53+
}
54+
}

src/main/java/gregtech/datafix/util/DataFixConstants.java

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ public final class DataFixConstants {
66
public static final String TILE_ENTITIES_TAG = "TileEntities";
77
public static final String SECTIONS = "Sections";
88

9+
public static final String LEVEL_CHUNK_X_POS = "xPos";
10+
public static final String LEVEL_CHUNK_Z_POS = "zPos";
11+
12+
public static final String CHUNK_SECTION_BLOCKS = "Blocks";
13+
public static final String CHUNK_SECTION_DATA = "Data";
14+
public static final String CHUNK_SECTION_ADD = "Add";
15+
public static final String CHUNK_SECTION_Y = "Y";
16+
17+
public static final int BLOCKS_PER_SECTION = 4096;
18+
919
public static final String ITEM_ID = "id";
1020
public static final String ITEM_COUNT = "Count";
1121
public static final String ITEM_DAMAGE = "Damage";

src/main/java/gregtech/integration/crafttweaker/material/CTMaterialRegistry.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ public class CTMaterialRegistry {
1616

1717
@ZenMethod
1818
public Material get(String modid, String name) {
19-
return GregTechAPI.materialManager.getRegistry(modid).getObject(name);
19+
if (GregTechAPI.materialManager.hasRegistry(modid)) {
20+
return GregTechAPI.materialManager.getRegistry(modid).getObject(name);
21+
}
22+
return null;
2023
}
2124

2225
@ZenMethod

0 commit comments

Comments
 (0)