Skip to content

Commit

Permalink
start of work
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 committed Jul 6, 2024
1 parent f7ad0bc commit e669b4b
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/items/toolitem/IGTTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ default Material getToolMaterial(ItemStack stack) {
String string = toolTag.getString(MATERIAL_KEY);
Material material = GregTechAPI.materialManager.getMaterial(string);
if (material == null) {
toolTag.setString(MATERIAL_KEY, (material = Materials.Iron).toString());
toolTag.setString(MATERIAL_KEY, (material = Materials.Iron).getRegistryName());
}
return material;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/items/toolitem/ToolHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static ItemStack getAndSetToolData(IGTTool tool, Material material, int m
ItemStack stack = tool.getRaw();
GTUtility.getOrCreateNbtCompound(stack).setInteger(HIDE_FLAGS, 2);
NBTTagCompound toolTag = getToolTag(stack);
toolTag.setString(MATERIAL_KEY, material.toString());
toolTag.setString(MATERIAL_KEY, material.getRegistryName());
toolTag.setInteger(MAX_DURABILITY_KEY, maxDurability);
toolTag.setInteger(HARVEST_LEVEL_KEY, harvestLevel);
toolTag.setFloat(TOOL_SPEED_KEY, toolSpeed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,19 @@ public interface IMaterialRegistryManager {
@NotNull
MaterialRegistry createRegistry(@NotNull String modid);

/**
* Check if a mod's registry exists. Accessible during all phases.
*
* @param modid the modid of the registry
* @return if the registry exists
*/
boolean hasRegistry(@NotNull String modid);

/**
* Get a mod's registry. Accessible during all phases.
*
* @param modid the modid of the mod
* @return the registry associated with the mod, or the GregTech registry if it does not have one
* @return the registry associated with the mod
*/
@NotNull
MaterialRegistry getRegistry(@NotNull String modid);
Expand All @@ -31,7 +39,7 @@ public interface IMaterialRegistryManager {
* Get a mod's registry. Accessible during all phases.
*
* @param networkId the network ID of the registry
* @return the registry associated with the network ID, or the GregTech registry if it does not have one
* @return the registry associated with the network ID
*/
@NotNull
MaterialRegistry getRegistry(int networkId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,31 @@ public MaterialRegistry createRegistry(@NotNull String modid) {
return registry;
}

@Override
public boolean hasRegistry(@NotNull String modid) {
return registries.containsKey(modid);
}

@NotNull
@Override
public MaterialRegistry getRegistry(@NotNull String modid) {
MaterialRegistry registry = registries.get(modid);
return registry != null ? registry : gregtechRegistry;
if (registry == null) {
throw new IllegalArgumentException("No Material Registry found for modid: " + modid);
}

return registry;
}

@NotNull
@Override
public MaterialRegistry getRegistry(int networkId) {
MaterialRegistry registry = networkIds.get(networkId);
return registry != null ? registry : gregtechRegistry;
if (registry == null) {
throw new IllegalArgumentException("No Material Registry found for network id: " + networkId);
}

return registry;
}

@NotNull
Expand All @@ -92,17 +105,23 @@ public Collection<Material> getRegisteredMaterials() {
@Override
public Material getMaterial(@NotNull String name) {
if (!name.isEmpty()) {
String modid;
MaterialRegistry registry;
String materialName;
int index = name.indexOf(':');
if (index >= 0) {
modid = name.substring(0, index);
materialName = name.substring(index + 1);
String modid = name.substring(0, index);
if (hasRegistry(modid)) {
registry = getRegistry(modid);
materialName = name.substring(index + 1);
} else {
return null;
}
} else {
modid = GTValues.MODID;
registry = gregtechRegistry;
materialName = name;
}
return getRegistry(modid).getObject(materialName);

return registry.getObject(materialName);
}
return null;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/gregtech/datafix/GTDataFixers.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gregtech.api.metatileentity.registry.MTERegistry;
import gregtech.datafix.migration.impl.MigrateMTEBlockTE;
import gregtech.datafix.migration.impl.MigrateMTEItems;
import gregtech.datafix.migration.impl.MigrateMaterialBlock;
import gregtech.datafix.migration.lib.MTERegistriesMigrator;
import gregtech.datafix.walker.WalkItemStackLike;

Expand Down Expand Up @@ -58,6 +59,10 @@ private static void registerFixes(@NotNull GTDataVersion version, @NotNull ModFi
fixer.registerFix(GTFixType.ITEM_STACK_LIKE, new MigrateMTEItems(migrator));
fixer.registerFix(FixTypes.CHUNK, new MigrateMTEBlockTE(migrator));
}
case V2_POST_MATERIALS -> {
// fixer.registerFix(GTFixType.ITEM_STACK_LIKE, new MigrateMaterialBlock());
fixer.registerFix(FixTypes.CHUNK, new MigrateMaterialBlock());
}
default -> {}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/gregtech/datafix/GTDataVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ public enum GTDataVersion {
/**
* Version of data after multiple MTE registries were possible
*/
V1_POST_MTE;
V1_POST_MTE,
/**
* Version of data after independent material registries were required
*/
V2_POST_MATERIALS
;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public class MigrateMTEBlockTE implements IFixableData {
private static final String X = "x";
private static final String Y = "y";
private static final String Z = "z";
private static final String X_POS = "xPos";
private static final String Z_POS = "zPos";
private static final String CHUNK_SECTION_Y = "Y";
private static final String CHUNK_SECTION_BLOCKS = "Blocks";
private static final String CHUNK_SECTION_DATA = "Data";
private static final String CHUNK_SECTION_ADD = "Add";

private static final int BLOCKS_PER_SECTION = 4096;

private final MTEMigrator migrator;

Expand Down Expand Up @@ -101,7 +93,7 @@ private static void processChunkSections(@NotNull NBTTagCompound level,
}

var blockStateIDMap = GameData.getBlockStateIDMap();
ChunkPos chunkPos = new ChunkPos(level.getInteger(X_POS), level.getInteger(Z_POS));
ChunkPos chunkPos = new ChunkPos(level.getInteger(LEVEL_CHUNK_X_POS), level.getInteger(LEVEL_CHUNK_Z_POS));
NBTTagList sectionTagList = level.getTagList(SECTIONS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < sectionTagList.tagCount(); i++) {
NBTTagCompound chunkSectionTag = sectionTagList.getCompoundTagAt(i);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package gregtech.datafix.migration.impl;

import net.minecraft.block.Block;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.datafix.IFixableData;

import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.chunk.NibbleArray;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistry;
import net.minecraftforge.registries.GameData;

import org.jetbrains.annotations.NotNull;

import static gregtech.datafix.util.DataFixConstants.*;

public class MigrateMaterialBlock implements IFixableData {

@Override
public int getFixVersion() {
return 2;
}

@Override
public @NotNull NBTTagCompound fixTagCompound(@NotNull NBTTagCompound compound) {
var blockStateIDMap = GameData.getBlockStateIDMap();
var r = ((ForgeRegistry<Block>) ForgeRegistries.BLOCKS);
NBTTagCompound level = compound.getCompoundTag(LEVEL_TAG);
ChunkPos chunkPos = new ChunkPos(level.getInteger(LEVEL_CHUNK_X_POS), level.getInteger(LEVEL_CHUNK_Z_POS));
NBTTagList sectionTagList = level.getTagList(SECTIONS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < sectionTagList.tagCount(); i++) {
NBTTagCompound chunkSectionTag = sectionTagList.getCompoundTagAt(i);

int sectionY = chunkSectionTag.getByte(CHUNK_SECTION_Y);
byte[] blockIDs = chunkSectionTag.getByteArray(CHUNK_SECTION_BLOCKS);
NibbleArray blockData = new NibbleArray(chunkSectionTag.getByteArray(CHUNK_SECTION_DATA));
NibbleArray extendedIDs = chunkSectionTag.hasKey(CHUNK_SECTION_ADD, Constants.NBT.TAG_BYTE_ARRAY) ?
new NibbleArray(chunkSectionTag.getByteArray(CHUNK_SECTION_ADD)) : null;
for (int j = 0; j < BLOCKS_PER_SECTION; j++) {
int x = j & 0x0F;
int y = j >> 8 & 0x0F;
int z = j >> 4 & 0x0F;

BlockPos pos = chunkPos.getBlock(x, sectionY << 4 | y, z);

}
}

return compound;
}
}
10 changes: 10 additions & 0 deletions src/main/java/gregtech/datafix/util/DataFixConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ public final class DataFixConstants {
public static final String TILE_ENTITIES_TAG = "TileEntities";
public static final String SECTIONS = "Sections";

public static final String LEVEL_CHUNK_X_POS = "xPos";
public static final String LEVEL_CHUNK_Z_POS = "zPos";

public static final String CHUNK_SECTION_BLOCKS = "Blocks";
public static final String CHUNK_SECTION_DATA = "Data";
public static final String CHUNK_SECTION_ADD = "Add";
public static final String CHUNK_SECTION_Y = "Y";

public static final int BLOCKS_PER_SECTION = 4096;

public static final String ITEM_ID = "id";
public static final String ITEM_COUNT = "Count";
public static final String ITEM_DAMAGE = "Damage";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public class CTMaterialRegistry {

@ZenMethod
public Material get(String modid, String name) {
return GregTechAPI.materialManager.getRegistry(modid).getObject(name);
if (GregTechAPI.materialManager.hasRegistry(modid)) {
return GregTechAPI.materialManager.getRegistry(modid).getObject(name);
}
return null;
}

@ZenMethod
Expand Down

0 comments on commit e669b4b

Please sign in to comment.