mirror of
https://github.com/GTNewHorizons/Postea.git
synced 2026-11-12 08:27:33 +00:00
Block replacement working, but only on reload, not initial generation.
This commit is contained in:
1 parent
0673c15af9
commit
a3d9aac95e
8 files changed
+135
-7
No files matched your search
@@ -0,0 +1,38 @@
|
||||
package com.colen.postea.API;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.item.Item;
|
||||
|
||||
public class BlockKey {
|
||||
|
||||
private final Block block;
|
||||
private final int meta;
|
||||
|
||||
public BlockKey(Block block, int meta) {
|
||||
this.block = block;
|
||||
this.meta = meta;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == null) return false;
|
||||
if (o instanceof BlockKey that) {
|
||||
return (this.meta == that.meta) && (this.block == that.block);
|
||||
} else return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(block, meta);
|
||||
}
|
||||
|
||||
public int getDamage() {
|
||||
return meta;
|
||||
}
|
||||
|
||||
public Block getBlock() {
|
||||
return block;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.colen.postea.API;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class BlockReplacementManager {
|
||||
|
||||
private static final Map<BlockKey, BlockKey> replacementMap = new HashMap<>();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void addBlockReplacement(Block blockIn, int metadataIn, Block blockOut, int metadataOut) {
|
||||
replacementMap.put(new BlockKey(blockIn, metadataIn), new BlockKey(blockOut, metadataOut));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static BlockKey getBlockReplacement(Block blockIn, int metadataIn) {
|
||||
return replacementMap.getOrDefault(new BlockKey(blockIn, metadataIn), null);
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -5,19 +5,19 @@ import java.util.Map;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
|
||||
public class ReplacementManager {
|
||||
public class ItemStackReplacementManager {
|
||||
|
||||
private static final Map<ItemStackKey, ItemStackKey> replacementMap = new HashMap<>();
|
||||
|
||||
// Public API for adding replacements.
|
||||
// Public API for adding replacement ItemStacks.
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static void addReplacement(Item itemIn, int metaIn, Item itemOut, int metaOut) {
|
||||
public static void addItemReplacement(Item itemIn, int metaIn, Item itemOut, int metaOut) {
|
||||
replacementMap.put(new ItemStackKey(itemIn, metaIn), new ItemStackKey(itemOut, metaOut));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public static ItemStackKey getReplacement(Item itemIn, int metaIn) {
|
||||
public static ItemStackKey getItemReplacement(Item itemIn, int metaIn) {
|
||||
return replacementMap.getOrDefault(new ItemStackKey(itemIn, metaIn), null);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,19 @@
|
||||
package com.colen.postea;
|
||||
|
||||
import com.colen.postea.API.BlockReplacementManager;
|
||||
import com.colen.postea.API.ItemStackReplacementManager;
|
||||
import cpw.mods.fml.common.Mod;
|
||||
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.Item;
|
||||
|
||||
@Mod(modid = Tags.MODID, version = Tags.VERSION, name = Tags.MODNAME, acceptedMinecraftVersions = "[1.7.10]")
|
||||
@SuppressWarnings("unused")
|
||||
public class Postea {
|
||||
|
||||
@Mod.EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event) {
|
||||
ItemStackReplacementManager.addItemReplacement(Item.getItemFromBlock(Blocks.grass), 0, Item.getItemFromBlock(Blocks.coal_ore), 10);
|
||||
BlockReplacementManager.addBlockReplacement(Blocks.bedrock, 0, Blocks.wool, 2);
|
||||
}
|
||||
}
|
||||
@@ -44,6 +44,7 @@ public final class PosteaLoadingPlugin implements IEarlyMixinLoader, IFMLLoading
|
||||
public List<String> getMixins(Set<String> loadedCoreMods) {
|
||||
final List<String> mixins = new ArrayList<>();
|
||||
mixins.add("MixinItemStack");
|
||||
mixins.add("MixinChunk");
|
||||
|
||||
return mixins;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.colen.postea.mixins.early;
|
||||
|
||||
import com.colen.postea.API.BlockKey;
|
||||
import com.colen.postea.API.BlockReplacementManager;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
|
||||
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
|
||||
@Mixin(AnvilChunkLoader.class)
|
||||
public abstract class MixinChunk {
|
||||
|
||||
@Inject(
|
||||
method = "readChunkFromNBT(Lnet/minecraft/world/World;Lnet/minecraft/nbt/NBTTagCompound;)Lnet/minecraft/world/chunk/Chunk;",
|
||||
at = @At("RETURN"),
|
||||
cancellable = true
|
||||
)
|
||||
private void modifyChunkData(World world, NBTTagCompound compound, CallbackInfoReturnable<Chunk> cir) {
|
||||
Chunk chunk = cir.getReturnValue();
|
||||
|
||||
// Iterate over each ExtendedBlockStorage. Kinda ugly but does the job...
|
||||
for (ExtendedBlockStorage storage : chunk.getBlockStorageArray()) {
|
||||
if (storage != null) { // Always check if storage isn't null before using it
|
||||
for (int x = 0; x < 16; x++) {
|
||||
for (int y = 0; y < 16; y++) { // This is not a mistake, we do this on purpose because of how minecraft stores stuff.
|
||||
for (int z = 0; z < 16; z++) {
|
||||
|
||||
// Get the current block and metadata at the position
|
||||
Block currentBlock = storage.getBlockByExtId(x, y, z);
|
||||
int currentMeta = storage.getExtBlockMetadata(x, y, z);
|
||||
|
||||
// Check if there's a replacement for this block+metadata combination
|
||||
BlockKey replacement = BlockReplacementManager.getBlockReplacement(currentBlock, currentMeta);
|
||||
|
||||
// If there's a replacement, set the new block and metadata
|
||||
if (replacement != null) {
|
||||
storage.func_150818_a(x, y, z, replacement.getBlock());
|
||||
storage.setExtBlockMetadata(x, y, z, replacement.getDamage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cir.setReturnValue(chunk);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import org.spongepowered.asm.mixin.injection.Inject;
|
||||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
|
||||
|
||||
import com.colen.postea.API.ItemStackKey;
|
||||
import com.colen.postea.API.ReplacementManager;
|
||||
import com.colen.postea.API.ItemStackReplacementManager;
|
||||
|
||||
@Mixin(ItemStack.class)
|
||||
public abstract class MixinItemStack {
|
||||
@@ -24,7 +24,7 @@ public abstract class MixinItemStack {
|
||||
|
||||
@Inject(method = "<init>(Lnet/minecraft/item/Item;II)V", at = @At("RETURN"))
|
||||
private void onConstructorReturn(Item item, int size, int metadata, CallbackInfo ci) {
|
||||
ItemStackKey replacement = ReplacementManager.getReplacement(item, metadata);
|
||||
ItemStackKey replacement = ItemStackReplacementManager.getItemReplacement(item, metadata);
|
||||
|
||||
if (replacement == null) return;
|
||||
|
||||
@@ -35,7 +35,7 @@ public abstract class MixinItemStack {
|
||||
|
||||
@Inject(method = "readFromNBT(Lnet/minecraft/nbt/NBTTagCompound;)V", at = @At("RETURN"))
|
||||
private void onReadFromNBTReturn(NBTTagCompound p_77963_1_, CallbackInfo ci) {
|
||||
ItemStackKey replacement = ReplacementManager.getReplacement(field_151002_e, itemDamage);
|
||||
ItemStackKey replacement = ItemStackReplacementManager.getItemReplacement(field_151002_e, itemDamage);
|
||||
|
||||
if (replacement == null) return;
|
||||
|
||||
|
||||
Reference in new issue
Block a user