From 2ad55d3e6c3fb7915a12be4291887373522b878f Mon Sep 17 00:00:00 2001 From: D-Cysteine <54219287+D-Cysteine@users.noreply.github.com> Date: Tue, 19 Oct 2021 22:30:24 -0600 Subject: Detect the ore type and replace with that type of cobble --- src/main/java/gregtech/api/util/GT_Utility.java | 51 +++++++++++++++++++++- .../common/blocks/GT_Block_Ores_Abstract.java | 18 ++++++++ .../machines/basic/GT_MetaTileEntity_Miner.java | 29 ++++++++---- .../GT_MetaTileEntity_OreDrillingPlantBase.java | 28 ++++++++---- 4 files changed, 107 insertions(+), 19 deletions(-) (limited to 'src/main/java/gregtech') diff --git a/src/main/java/gregtech/api/util/GT_Utility.java b/src/main/java/gregtech/api/util/GT_Utility.java index 64a60e8630..36925cbe5b 100644 --- a/src/main/java/gregtech/api/util/GT_Utility.java +++ b/src/main/java/gregtech/api/util/GT_Utility.java @@ -1,6 +1,7 @@ package gregtech.api.util; import cofh.api.transport.IItemDuct; +import com.google.common.base.Suppliers; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; import com.gtnewhorizon.structurelib.alignment.IAlignment; @@ -28,6 +29,7 @@ import gregtech.api.objects.ItemData; import gregtech.api.threads.GT_Runnable_Sound; import gregtech.api.util.extensions.ArrayExt; import gregtech.common.GT_Proxy; +import gregtech.common.blocks.GT_Block_Ores_Abstract; import ic2.api.recipe.IRecipeInput; import ic2.api.recipe.RecipeInputItemStack; import ic2.api.recipe.RecipeInputOreDict; @@ -80,7 +82,9 @@ import java.text.DecimalFormat; import java.text.DecimalFormatSymbols; import java.util.*; import java.util.Map.Entry; +import java.util.function.Function; import java.util.function.IntFunction; +import java.util.function.Supplier; import static gregtech.GT_Mod.GT_FML_LOGGER; import static gregtech.api.enums.GT_Values.*; @@ -103,6 +107,8 @@ public class GT_Utility { private static final Map sFilledContainerToData = new /*Concurrent*/HashMap<>(); private static final Map> sEmptyContainerToFluidToData = new /*Concurrent*/HashMap<>(); private static final Map> sFluidToContainers = new HashMap<>(); + /** Must use {@code Supplier} here because the ore prefixes have not yet been registered at class load time. */ + private static final Map> sOreToCobble = new HashMap<>(); public static volatile int VERSION = 509; public static boolean TE_CHECK = false, BC_CHECK = false, CHECK_ALL = true, RF_CHECK = false; public static Map sPlayedSoundMap = new /*Concurrent*/HashMap<>(); @@ -117,6 +123,29 @@ public class GT_Utility { GregTech_API.sItemStackMappings.add(sFilledContainerToData); GregTech_API.sItemStackMappings.add(sEmptyContainerToFluidToData); + + // 1 is the magic index to get the cobblestone block. + // See: GT_Block_Stones.java, GT_Block_Granites.java + Function> materialToCobble = + m -> Suppliers.memoize(() -> GT_OreDictUnificator.getOres(OrePrefixes.stone, m).get(1))::get; + sOreToCobble.put( + OrePrefixes.oreBlackgranite, + materialToCobble.apply(Materials.GraniteBlack)); + sOreToCobble.put( + OrePrefixes.oreRedgranite, + materialToCobble.apply(Materials.GraniteRed)); + sOreToCobble.put( + OrePrefixes.oreMarble, + materialToCobble.apply(Materials.Marble)); + sOreToCobble.put( + OrePrefixes.oreBasalt, + materialToCobble.apply(Materials.Basalt)); + sOreToCobble.put( + OrePrefixes.oreNetherrack, + () -> new ItemStack(Blocks.netherrack)); + sOreToCobble.put( + OrePrefixes.oreEndstone, + () -> new ItemStack(Blocks.end_stone)); } public static int safeInt(long number, int margin){ @@ -2733,7 +2762,9 @@ public class GT_Utility { ); public static boolean isOre(Block aBlock, int aMeta) { - return isOre(new ItemStack(aBlock, 1, aMeta)) || ORE_BLOCK_CLASSES.contains(aBlock.getClass().getName()); + return (aBlock instanceof GT_Block_Ores_Abstract) + || isOre(new ItemStack(aBlock, 1, aMeta)) + || ORE_BLOCK_CLASSES.contains(aBlock.getClass().getName()); } public static boolean isOre(ItemStack aStack) { @@ -2744,6 +2775,24 @@ public class GT_Utility { return false; } + /** + * Do NOT mutate the returned {@code ItemStack}! + * We return {@code ItemStack} instead of {@code Block} so that we can include metadata. + */ + public static ItemStack getCobbleForOre(Block ore, short metaData) { + // We need to convert to small ores to regular ores because small ores don't have associated ItemData. + // We take the modulus of the metadata by 16000 because that is the magic number to convert small ores to regular ores. + // See: GT_TileEntity_Ores.java + ItemData association = GT_OreDictUnificator.getAssociation(new ItemStack(Item.getItemFromBlock(ore), 1, metaData % 16000)); + if (association != null) { + Supplier supplier = sOreToCobble.get(association.mPrefix); + if (supplier != null) { + return supplier.get(); + } + } + return new ItemStack(Blocks.cobblestone); + } + public static Optional reverseShapelessRecipe(ItemStack output, Object... aRecipe) { if (output == null) { return Optional.empty(); diff --git a/src/main/java/gregtech/common/blocks/GT_Block_Ores_Abstract.java b/src/main/java/gregtech/common/blocks/GT_Block_Ores_Abstract.java index 48cd41187d..d862e5a555 100644 --- a/src/main/java/gregtech/common/blocks/GT_Block_Ores_Abstract.java +++ b/src/main/java/gregtech/common/blocks/GT_Block_Ores_Abstract.java @@ -12,6 +12,7 @@ import gregtech.api.items.GT_Generic_Block; import gregtech.api.util.GT_LanguageManager; import gregtech.api.util.GT_ModHandler; import gregtech.api.util.GT_OreDictUnificator; +import gregtech.api.util.GT_Utility; import gregtech.common.render.GT_Renderer_Block; import net.minecraft.block.Block; import net.minecraft.block.ITileEntityProvider; @@ -20,6 +21,7 @@ import net.minecraft.client.renderer.texture.IIconRegister; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.Entity; import net.minecraft.entity.boss.EntityDragon; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; @@ -134,6 +136,22 @@ public abstract class GT_Block_Ores_Abstract extends GT_Generic_Block implements return aMaterial.getDefaultLocalizedNameForItem(getLocalizedNameFormat(aMaterial)); } + @Override + public boolean onBlockActivated(World aWorld, int aX, int aY, int aZ, EntityPlayer aPlayer, int aSide, float par1, float par2, float par3) { + if (!aPlayer.isSneaking() || !aPlayer.capabilities.isCreativeMode) { + return false; + } + + TileEntity tTileEntity = aWorld.getTileEntity(aX, aY, aZ); + if (!(tTileEntity instanceof GT_TileEntity_Ores)) { + return false; + } + + boolean tNatural = (((GT_TileEntity_Ores) tTileEntity).mNatural = !((GT_TileEntity_Ores) tTileEntity).mNatural); + GT_Utility.sendChatToPlayer(aPlayer, "Ore \"mNatural\" flag set to: " + tNatural); + return true; + } + @Override public boolean onBlockEventReceived(World p_149696_1_, int p_149696_2_, int p_149696_3_, int p_149696_4_, int p_149696_5_, int p_149696_6_) { super.onBlockEventReceived(p_149696_1_, p_149696_2_, p_149696_3_, p_149696_4_, p_149696_5_, p_149696_6_); diff --git a/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Miner.java b/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Miner.java index d93d959955..6d4b160a86 100644 --- a/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Miner.java +++ b/src/main/java/gregtech/common/tileentities/machines/basic/GT_MetaTileEntity_Miner.java @@ -176,18 +176,21 @@ public class GT_MetaTileEntity_Miner extends GT_MetaTileEntity_BasicMachine { moveOneDown(aBaseMetaTileEntity); } else { ChunkPosition oreBlockPos; - Block block; + int x = 0, y = 0, z = 0; + Block oreBlock; + int oreBlockMetadata = 0; do { oreBlockPos = oreBlockPositions.remove(0); - block = aBaseMetaTileEntity.getBlockOffset(oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ); + oreBlock = aBaseMetaTileEntity.getBlockOffset(oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ); + x = aBaseMetaTileEntity.getXCoord() + oreBlockPos.chunkPosX; + y = aBaseMetaTileEntity.getYCoord() + oreBlockPos.chunkPosY; + z = aBaseMetaTileEntity.getZCoord() + oreBlockPos.chunkPosZ; + oreBlockMetadata = getBaseMetaTileEntity().getWorld().getBlockMetadata(x, y, z); } // someone else might have removed the block - while (block == Blocks.air && !oreBlockPositions.isEmpty()); + while (!GT_Utility.isOre(oreBlock, oreBlockMetadata) && !oreBlockPositions.isEmpty()); - if (block != Blocks.air) { - mineBlock(aBaseMetaTileEntity, block, - aBaseMetaTileEntity.getXCoord() + oreBlockPos.chunkPosX, - aBaseMetaTileEntity.getYCoord() + oreBlockPos.chunkPosY, - aBaseMetaTileEntity.getZCoord() + oreBlockPos.chunkPosZ); + if (GT_Utility.isOre(oreBlock, oreBlockMetadata)) { + mineBlock(aBaseMetaTileEntity, oreBlock, x, y, z); } } } @@ -265,7 +268,15 @@ public class GT_MetaTileEntity_Miner extends GT_MetaTileEntity_BasicMachine { mOutputItems[0] = drops.get(0); if (drops.size() > 1) mOutputItems[1] = drops.get(1); - aBaseMetaTileEntity.getWorld().setBlock(x, y, z, Blocks.cobblestone); + + short metaData = 0; + TileEntity tTileEntity = getBaseMetaTileEntity().getTileEntity(x, y, z); + if (tTileEntity instanceof GT_TileEntity_Ores) { + metaData = ((GT_TileEntity_Ores) tTileEntity).mMetaData; + } + + ItemStack cobble = GT_Utility.getCobbleForOre(block, metaData); + aBaseMetaTileEntity.getWorld().setBlock(x, y, z, Block.getBlockFromItem(cobble.getItem()), cobble.getItemDamage(), 3); if (debugBlockMiner) GT_Log.out.println("MINER: Mining GT ore block at " + x + " " + y + " " + z); } diff --git a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java index 1643eb4f8d..50f28a83b4 100644 --- a/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java +++ b/src/main/java/gregtech/common/tileentities/machines/multi/GT_MetaTileEntity_OreDrillingPlantBase.java @@ -17,7 +17,6 @@ import gregtech.common.blocks.GT_TileEntity_Ores; import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.InventoryPlayer; -import net.minecraft.init.Blocks; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; @@ -32,8 +31,6 @@ import java.util.Collection; import java.util.Collections; import java.util.HashSet; -import org.lwjgl.input.Keyboard; - import static gregtech.api.enums.GT_Values.VN; public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTileEntity_DrillerBase { @@ -109,21 +106,34 @@ public abstract class GT_MetaTileEntity_OreDrillingPlantBase extends GT_MetaTile } private boolean processOreList(){ ChunkPosition oreBlockPos = null; + int x = 0, y = 0, z = 0; Block oreBlock = null; + int oreBlockMetadata = 0; - while ((oreBlock == null || oreBlock == Blocks.air) && !oreBlockPositions.isEmpty()) { + while ((oreBlock == null || !GT_Utility.isOre(oreBlock, oreBlockMetadata)) && !oreBlockPositions.isEmpty()) { oreBlockPos = oreBlockPositions.remove(0); - if (GT_Utility.eraseBlockByFakePlayer(getFakePlayer(getBaseMetaTileEntity()), oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ, true)) - oreBlock = getBaseMetaTileEntity().getBlock(oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ); + x = oreBlockPos.chunkPosX; + y = oreBlockPos.chunkPosY; + z = oreBlockPos.chunkPosZ; + if (GT_Utility.eraseBlockByFakePlayer(getFakePlayer(getBaseMetaTileEntity()), x, y, z, true)) + oreBlock = getBaseMetaTileEntity().getBlock(x, y, z); + oreBlockMetadata = getBaseMetaTileEntity().getWorld().getBlockMetadata(x, y, z); } if (!tryConsumeDrillingFluid()) { oreBlockPositions.add(0, oreBlockPos); return false; } - if (oreBlock != null && oreBlock != Blocks.air) { - Collection oreBlockDrops = getBlockDrops(oreBlock, oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ); - getBaseMetaTileEntity().getWorld().setBlock(oreBlockPos.chunkPosX, oreBlockPos.chunkPosY, oreBlockPos.chunkPosZ, Blocks.cobblestone); + if (oreBlock != null && GT_Utility.isOre(oreBlock, oreBlockMetadata)) { + short metaData = 0; + TileEntity tTileEntity = getBaseMetaTileEntity().getTileEntity(x, y, z); + if (tTileEntity instanceof GT_TileEntity_Ores) { + metaData = ((GT_TileEntity_Ores) tTileEntity).mMetaData; + } + + Collection oreBlockDrops = getBlockDrops(oreBlock, x, y, z); + ItemStack cobble = GT_Utility.getCobbleForOre(oreBlock, metaData); + getBaseMetaTileEntity().getWorld().setBlock(x, y, z, Block.getBlockFromItem(cobble.getItem()), cobble.getItemDamage(), 3); mOutputItems = getOutputByDrops(oreBlockDrops); } return true; -- cgit