Skip to content

Commit

Permalink
Address requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed May 8, 2021
1 parent 2a0a76b commit dec3f00
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package gregtech.common.asm;

import gregtech.common.asm.util.ObfMapping;
Expand Down
38 changes: 0 additions & 38 deletions src/main/java/gregtech/common/blocks/MetaBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,44 +237,6 @@ protected static void createGeneratedBlock(Predicate<Material> materialPredicate
blocksToGenerate.forEach((key, value) -> blockGenerator.accept(value, key));
}

/**
* Old generation code for initializing BlockCompressed and BlockSurfaceRockDeprecated.
*
* Needed for accurately reproducing the deprecated MetaBlock packing behavior for determining what the old blocks
* are so we can remap them to the correct replacements.
*
* @param materialPredicate a filter for determining if a Material qualifies for generation in the category.
* @param blockGenerator a function which accepts a Materials set to pack into a MetaBlock, and the ordinal this
* MetaBlock should have within its category.
* @return the number of blocks generated by this request
*/
@Deprecated
protected static int createGeneratedBlockDeprecated(Predicate<Material> materialPredicate,
BiConsumer<Material[], Integer> blockGenerator) {
Material[] materialBuffer = new Material[16];
Arrays.fill(materialBuffer, Materials._NULL);
int currentGenerationIndex = 0;
for (Material material : Material.MATERIAL_REGISTRY) {
if (materialPredicate.test(material)) {
if (currentGenerationIndex > 0 && currentGenerationIndex % 16 == 0) {
blockGenerator.accept(materialBuffer, currentGenerationIndex / 16 - 1);
Arrays.fill(materialBuffer, Materials._NULL);
}
materialBuffer[currentGenerationIndex % 16] = material;
currentGenerationIndex++;
}
}
if (materialBuffer[0] != Materials._NULL) {
blockGenerator.accept(materialBuffer, currentGenerationIndex / 16);
}
return (currentGenerationIndex / 16) + 1;
}

private static BiConsumer<Material[], Integer> generateInto(TIntObjectMap<int[]> dest) {
return (materials, index) -> dest.put(
index, Arrays.stream(materials).mapToInt(Material.MATERIAL_REGISTRY::getIDForObject).toArray());
}

private static void createSurfaceRockBlock(Material[] materials, int index) {
BlockSurfaceRockDeprecated block = new BlockSurfaceRockDeprecated(materials);
block.setRegistryName("meta_block_surface_rock_" + index);
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gregtech/common/datafix/GregTechDataFixers.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class GregTechDataFixers {
*/
public static final int DATA_VERSION = V1_META_BLOCK_ID_REWORK;

/**
* Constants used for fixing NBT data.
*/
public static final String COMPOUND_ID = "id";
public static final String COMPOUND_META = "Damage";

public static void init() {
CompoundDataFixer fmlFixer = FMLCommonHandler.instance().getDataFixer();
IDataWalker walkItemStackLike = new WalkItemStackLike();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.common.datafix.fixes;

import gregtech.common.datafix.GregTechDataFixers;
import gregtech.common.datafix.fixes.metablockid.MetaBlockIdFixHelper;
import gregtech.common.datafix.fixes.metablockid.PreGraniteMetaBlockIdFixer;
import gregtech.common.datafix.fixes.metablockid.WorldDataHooks;
Expand All @@ -16,17 +17,17 @@ public int getFixVersion() {

@Override
public NBTTagCompound fixTagCompound(NBTTagCompound compound) {
if (!WorldDataHooks.isFixerAvailable()) {
if (WorldDataHooks.isFixerUnavailable()) {
return compound;
}

int index = MetaBlockIdFixHelper.getCompressedIndexFromResLoc(compound.getString("id"));
int index = MetaBlockIdFixHelper.getCompressedIndexFromResLoc(compound.getString(GregTechDataFixers.COMPOUND_ID));
if (index != -1) {
RemappedBlock remapped = ((PreGraniteMetaBlockIdFixer) WorldDataHooks.getMetaBlockIdFixer())
.remapCompressedPreGraniteToPost(index, compound.getShort("Damage"));
.remapCompressedPreGraniteToPost(index, compound.getShort(GregTechDataFixers.COMPOUND_META));
if (remapped != null) {
compound.setString("id", MetaBlockIdFixHelper.COMP_RESLOC_PREF + remapped.id);
compound.setShort("Damage", remapped.data);
compound.setString(GregTechDataFixers.COMPOUND_ID, MetaBlockIdFixHelper.COMP_RESLOC_PREF + remapped.id);
compound.setShort(GregTechDataFixers.COMPOUND_META, remapped.data);
}
}
return compound;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public int getFixVersion() {

@Override
public NBTTagCompound fixTagCompound(NBTTagCompound compound) {
if (!WorldDataHooks.isFixerAvailable()) {
if (WorldDataHooks.isFixerUnavailable()) {
return compound;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gregtech.common.datafix.fixes;

import gregtech.common.datafix.GregTechDataFixers;
import gregtech.common.datafix.fixes.metablockid.MetaBlockIdFixHelper;
import gregtech.common.datafix.fixes.metablockid.PostGraniteMetaBlockIdFixer;
import gregtech.common.datafix.fixes.metablockid.WorldDataHooks;
Expand All @@ -25,26 +26,26 @@ public int getFixVersion() {

@Override
public NBTTagCompound fixTagCompound(NBTTagCompound compound) {
if (!WorldDataHooks.isFixerAvailable()) {
if (WorldDataHooks.isFixerUnavailable()) {
return compound;
}

String blockResLoc = compound.getString("id");
String blockResLoc = compound.getString(GregTechDataFixers.COMPOUND_ID);
int index = MetaBlockIdFixHelper.getCompressedIndexFromResLoc(blockResLoc);
if (index != -1) {
RemappedBlock remapped = ((PostGraniteMetaBlockIdFixer) WorldDataHooks.getMetaBlockIdFixer())
.remapCompressedPostGraniteToNew(index, compound.getShort("Damage"));
compound.setString("id", MetaBlockIdFixHelper.COMP_RESLOC_PREF_NEW + remapped.id);
compound.setShort("Damage", remapped.data);
.remapCompressedPostGraniteToNew(index, compound.getShort(GregTechDataFixers.COMPOUND_META));
compound.setString(GregTechDataFixers.COMPOUND_ID, MetaBlockIdFixHelper.COMP_RESLOC_PREF_NEW + remapped.id);
compound.setShort(GregTechDataFixers.COMPOUND_META, remapped.data);
return compound;
}

index = MetaBlockIdFixHelper.getSurfRockIndexFromResLoc(blockResLoc);
if (index != -1) {
RemappedBlock remapped = ((PostGraniteMetaBlockIdFixer) WorldDataHooks.getMetaBlockIdFixer())
.remapSurfRockToNew(index, compound.getShort("Damage"));
compound.setString("id", MetaBlockIdFixHelper.SURF_ROCK_RESLOC_PREF_NEW + remapped.id);
compound.setShort("Damage", remapped.data);
.remapSurfRockToNew(index, compound.getShort(GregTechDataFixers.COMPOUND_META));
compound.setString(GregTechDataFixers.COMPOUND_ID, MetaBlockIdFixHelper.SURF_ROCK_RESLOC_PREF_NEW + remapped.id);
compound.setShort(GregTechDataFixers.COMPOUND_META, remapped.data);
}
return compound;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public int getFixVersion() {

@Override
public NBTTagCompound fixTagCompound(NBTTagCompound compound) {
if (!WorldDataHooks.isFixerAvailable()) {
if (WorldDataHooks.isFixerUnavailable()) {
return compound;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public int getNewId(int index) {
if (id != -1) {
return id;
}
return newIdCache[index] = Block.getIdFromBlock(Objects.requireNonNull(ForgeRegistries.BLOCKS.getValue(
newIdCache[index] = Block.getIdFromBlock(Objects.requireNonNull(ForgeRegistries.BLOCKS.getValue(
new ResourceLocation(GTValues.MODID, newNamePrefix + index))));

return newIdCache[index];
}

public NBTTagCompound serialize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public class WorldDataHooks {
@Nullable
private static MetaBlockIdFixer metaBlockIdFixer = null;

public static boolean isFixerAvailable() {
return metaBlockIdFixer != null;
private WorldDataHooks() {
}

public static boolean isFixerUnavailable() {
return metaBlockIdFixer == null;
}

public static MetaBlockIdFixer getMetaBlockIdFixer() {
Expand Down
39 changes: 36 additions & 3 deletions src/test/java/gregtech/common/blocks/MetaBlocksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private static BiConsumer<Material[], Integer> generateMappingSet(Predicate<Mate
@Test
public void simulateOldSurfaceRocksMapping() {
Set<Mapping> results = new TreeSet<>();
MetaBlocks.createGeneratedBlockDeprecated(
createGeneratedBlockDeprecated(
material -> material instanceof IngotMaterial && material.hasFlag(DustMaterial.MatFlags.GENERATE_ORE),
generateMappingSet(m -> m instanceof IngotMaterial, results));

Expand Down Expand Up @@ -106,7 +106,7 @@ private static void printDiffMappings(Set<Mapping> mappings, Set<Mapping> mappin

private static Set<Mapping> simulateOldCompressedBlockMapping(final Set<Material> toIgnore) {
final Set<Mapping> oldMappings = new TreeSet<>();
MetaBlocks.createGeneratedBlockDeprecated(
createGeneratedBlockDeprecated(
material -> !toIgnore.contains(material) &&
material instanceof DustMaterial &&
!OrePrefix.block.isIgnored(material),
Expand All @@ -127,4 +127,37 @@ public void simulateNewBlockMappings() {
System.out.println("--- New Compressed Block Mappings ---");
results.forEach(blockPrint);
}
}

/**
* Old generation code for initializing BlockCompressed and BlockSurfaceRockDeprecated.
*
* Needed for accurately reproducing the deprecated MetaBlock packing behavior for determining what the old blocks
* are so we can remap them to the correct replacements.
*
* @param materialPredicate a filter for determining if a Material qualifies for generation in the category.
* @param blockGenerator a function which accepts a Materials set to pack into a MetaBlock, and the ordinal this
* MetaBlock should have within its category.
* @return the number of blocks generated by this request
*/
@Deprecated
protected static int createGeneratedBlockDeprecated(Predicate<Material> materialPredicate,
BiConsumer<Material[], Integer> blockGenerator) {
Material[] materialBuffer = new Material[16];
Arrays.fill(materialBuffer, Materials._NULL);
int currentGenerationIndex = 0;
for (Material material : Material.MATERIAL_REGISTRY) {
if (materialPredicate.test(material)) {
if (currentGenerationIndex > 0 && currentGenerationIndex % 16 == 0) {
blockGenerator.accept(materialBuffer, currentGenerationIndex / 16 - 1);
Arrays.fill(materialBuffer, Materials._NULL);
}
materialBuffer[currentGenerationIndex % 16] = material;
currentGenerationIndex++;
}
}
if (materialBuffer[0] != Materials._NULL) {
blockGenerator.accept(materialBuffer, currentGenerationIndex / 16);
}
return (currentGenerationIndex / 16) + 1;
}
}

0 comments on commit dec3f00

Please sign in to comment.