Several mimic fixes, with actually cleaned up commit history. (#1379)

* Reimplemented a lot of mimic logic and did a first run of changing how mimics store their states.

* Finished removing all metadata calls for blocks replaced by mimics.

* Update EntityMimic.java

* Update ItemBlockMimic.java

* Update TileMimic.java

* How did I even replace a semicolon with a slash.

* Changed all tabs to 4 spaces. Changed Serializer to StateUtil

* Fixed spacing again, hopefully for the last time
This commit is contained in:
KohaiKhaos 2018-08-07 18:28:55 -05:00 committed by Nick Ignoffo
parent 7954d04421
commit ebfd8ce9a3
6 changed files with 238 additions and 52 deletions

View file

@ -15,6 +15,7 @@ import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
import WayofTime.bloodmagic.core.RegistrarBloodMagicRecipes;
import WayofTime.bloodmagic.incense.EnumTranquilityType;
import WayofTime.bloodmagic.incense.TranquilityStack;
import WayofTime.bloodmagic.util.StateUtil;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.BlockStateContainer;
@ -116,7 +117,7 @@ public class BloodMagicCorePlugin implements IBloodMagicPlugin
if (blockData.length > 1)
{ // We have properties listed, so let's build a state.
api.getBlacklist().addTeleposer(parseState(value));
api.getBlacklist().addTeleposer(StateUtil.parseState(value));
continue;
}
@ -136,7 +137,7 @@ public class BloodMagicCorePlugin implements IBloodMagicPlugin
if (blockData.length > 1)
{ // We have properties listed, so let's build a state.
api.getBlacklist().addTeleposer(parseState(value));
api.getBlacklist().addTeleposer(StateUtil.parseState(value));
continue;
}
@ -152,29 +153,4 @@ public class BloodMagicCorePlugin implements IBloodMagicPlugin
api.getBlacklist().addWellOfSuffering(entityEntry.getRegistryName());
}
}
private static IBlockState parseState(String blockInfo)
{
String[] split = blockInfo.split("\\[");
split[1] = split[1].substring(0, split[1].lastIndexOf("]")); // Make sure brackets are removed from state
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(split[0])); // Find the block
if (block == Blocks.AIR)
return Blocks.AIR.getDefaultState(); // The block is air, so we're looking at invalid data
BlockStateContainer blockState = block.getBlockState();
IBlockState returnState = blockState.getBaseState();
// Force our values into the state
String[] stateValues = split[1].split(","); // Splits up each value
for (String value : stateValues)
{
String[] valueSplit = value.split("="); // Separates property and value
IProperty property = blockState.getProperty(valueSplit[0]);
if (property != null)
returnState = returnState.withProperty(property, (Comparable) property.parseValue(valueSplit[1]).get()); // Force the property into the state
}
return returnState;
}
}
}

View file

@ -8,6 +8,7 @@ import WayofTime.bloodmagic.block.enums.EnumMimic;
import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
import WayofTime.bloodmagic.tile.TileMimic;
import WayofTime.bloodmagic.util.Utils;
import WayofTime.bloodmagic.item.block.ItemBlockMimic;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
@ -59,7 +60,7 @@ public class BlockMimic extends BlockEnum<EnumMimic> implements IAltarComponent
if (mimicBlock == Blocks.AIR) {
return FULL_BLOCK_AABB;
}
IBlockState mimicState = mimicBlock.getStateFromMeta(tileMimic.metaOfReplacedBlock);
IBlockState mimicState = tileMimic.getReplacedState();
if (mimicBlock != this) {
return mimicState.getCollisionBoundingBox(world, pos);
}
@ -82,7 +83,7 @@ public class BlockMimic extends BlockEnum<EnumMimic> implements IAltarComponent
if (mimicBlock == Blocks.AIR) {
return FULL_BLOCK_AABB;
}
IBlockState mimicState = mimicBlock.getStateFromMeta(tileMimic.getStackInSlot(0).getItemDamage());
IBlockState mimicState = tileMimic.getReplacedState();
if (mimicBlock != this) {
return mimicState.getSelectedBoundingBox(world, pos);
}
@ -136,7 +137,7 @@ public class BlockMimic extends BlockEnum<EnumMimic> implements IAltarComponent
ItemStack stack = mimic.getStackInSlot(0);
if (stack.getItem() instanceof ItemBlock) {
Block block = ((ItemBlock) stack.getItem()).getBlock();
IBlockState mimicState = block.getStateFromMeta(stack.getItemDamage());
IBlockState mimicState = mimic.getReplacedState();
if (block != this) {
if (block.getRenderType(mimicState) == EnumBlockRenderType.ENTITYBLOCK_ANIMATED) {
return RegistrarBloodMagicBlocks.BLOOD_LIGHT.getDefaultState(); //Small and invisible-ish, basically this is returned in order to not render over the animated block (TESR)
@ -208,7 +209,7 @@ public class BlockMimic extends BlockEnum<EnumMimic> implements IAltarComponent
if (stack.getItem() instanceof ItemBlock) {
Block block = ((ItemBlock) stack.getItem()).getBlock();
if (block instanceof IAltarComponent) {
return ((IAltarComponent) block).getType(world, block.getStateFromMeta(mimic.metaOfReplacedBlock), pos);
return ((IAltarComponent) block).getType(world, mimic.getReplacedState(), pos);
} else {
for (ComponentType altarComponent : ComponentType.values())
if (block == Utils.getBlockForComponent(altarComponent))
@ -218,4 +219,10 @@ public class BlockMimic extends BlockEnum<EnumMimic> implements IAltarComponent
}
return null;
}
@Override
public ItemBlock getItem() {
return new ItemBlockMimic(this);
}
}

View file

@ -4,6 +4,7 @@ import WayofTime.bloodmagic.block.BlockMimic;
import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
import WayofTime.bloodmagic.entity.ai.EntityAIMimicReform;
import WayofTime.bloodmagic.tile.TileMimic;
import WayofTime.bloodmagic.util.StateUtil;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
@ -12,6 +13,7 @@ import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.*;
import net.minecraft.entity.monster.EntityIronGolem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.EntityEquipmentSlot;
@ -39,6 +41,7 @@ public class EntityMimic extends EntityDemonBase {
public boolean dropItemsOnBreak = true;
public NBTTagCompound tileTag = new NBTTagCompound();
public int metaOfReplacedBlock = 0;
public IBlockState stateOfReplacedBlock = Blocks.AIR.getDefaultState();
public int playerCheckRadius = 5;
public EntityMimic(World worldIn) {
@ -67,6 +70,7 @@ public class EntityMimic extends EntityDemonBase {
tag.setTag("tileTag", tileTag);
tag.setInteger("metaOfReplacedBlock", metaOfReplacedBlock);
tag.setInteger("playerCheckRadius", playerCheckRadius);
tag.setString("stateOfReplacedBlock", stateOfReplacedBlock.toString());
}
@Override
@ -77,6 +81,7 @@ public class EntityMimic extends EntityDemonBase {
tileTag = tag.getCompoundTag("tileTag");
metaOfReplacedBlock = tag.getInteger("metaOfReplacedBlock");
playerCheckRadius = tag.getInteger("playerCheckRadius");
stateOfReplacedBlock = StateUtil.parseState(tag.getString("stateOfReplacedBlock"));
}
public ItemStack getMimicItemStack() {
@ -88,7 +93,7 @@ public class EntityMimic extends EntityDemonBase {
}
public boolean spawnHeldBlockOnDeath(World world, BlockPos pos) {
return world.isAirBlock(pos) && TileMimic.replaceMimicWithBlockActual(world, pos, getMimicItemStack(), tileTag, metaOfReplacedBlock);
return world.isAirBlock(pos) && TileMimic.replaceMimicWithBlockActual(world, pos, getMimicItemStack(), tileTag, stateOfReplacedBlock);
}
public boolean spawnMimicBlockAtPosition(World world, BlockPos pos) {
@ -98,7 +103,7 @@ public class EntityMimic extends EntityDemonBase {
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileMimic) {
TileMimic mimic = (TileMimic) tile;
mimic.metaOfReplacedBlock = metaOfReplacedBlock;
mimic.setReplacedState(this.stateOfReplacedBlock);
mimic.tileTag = tileTag;
mimic.setInventorySlotContents(0, getMimicItemStack());
mimic.dropItemsOnBreak = dropItemsOnBreak;
@ -111,11 +116,11 @@ public class EntityMimic extends EntityDemonBase {
return false;
}
public void initializeMimic(ItemStack heldStack, NBTTagCompound tileTag, boolean dropItemsOnBreak, int metaOfReplacedBlock, int playerCheckRadius, BlockPos homePosition) {
public void initializeMimic(ItemStack heldStack, NBTTagCompound tileTag, boolean dropItemsOnBreak, IBlockState stateOfReplacedBlock, int playerCheckRadius, BlockPos homePosition) {
this.setMimicItemStack(heldStack);
this.tileTag = tileTag;
this.dropItemsOnBreak = dropItemsOnBreak;
this.metaOfReplacedBlock = metaOfReplacedBlock;
this.stateOfReplacedBlock = stateOfReplacedBlock;
this.playerCheckRadius = playerCheckRadius;
this.setHomePosAndDistance(homePosition, 2); //TODO: Save this.
}
@ -319,4 +324,4 @@ public class EntityMimic extends EntityDemonBase {
super(spider, classTarget, true);
}
}
}
}

View file

@ -0,0 +1,144 @@
package WayofTime.bloodmagic.item.block;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import WayofTime.bloodmagic.block.BlockMimic;
import WayofTime.bloodmagic.tile.TileMimic;
import WayofTime.bloodmagic.block.base.BlockEnum;
import WayofTime.bloodmagic.item.block.base.ItemBlockEnum;
import WayofTime.bloodmagic.util.ChatUtil;
public class ItemBlockMimic extends ItemBlockEnum
{
public ItemBlockMimic(BlockEnum block)
{
super(block);
setHasSubtypes(true);
}
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
ItemStack stack = player.getHeldItem(hand);
//If not sneaking, do normal item use
if (!player.isSneaking())
{
return super.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);
}
//IF sneaking and player has permission, replace the targeted block
if (player.canPlayerEdit(pos, facing, stack))
{
//Store information about the block being replaced and its appropriate itemstack
IBlockState replacedBlockstate = world.getBlockState(pos);
Block replacedBlock = replacedBlockstate.getBlock();
ItemStack replacedStack = replacedBlock.getItem(world, pos, replacedBlockstate);
//Get the state for the mimic
IBlockState mimicBlockstate = this.getBlock().getStateFromMeta(stack.getMetadata());
//Check if the block can be replaced
if (!canReplaceBlock(world, pos, replacedBlockstate))
{
return super.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);
}
//Check if the tile entity, if any, can be replaced
TileEntity tileReplaced = world.getTileEntity(pos);
if (!canReplaceTile(tileReplaced))
{
return EnumActionResult.FAIL;
}
//If tile can be replaced, store info about the tile
NBTTagCompound tileTag = getTagFromTileEntity(tileReplaced);
if (tileReplaced != null)
{
NBTTagCompound voidTag = new NBTTagCompound();
voidTag.setInteger("x", pos.getX());
voidTag.setInteger("y", pos.getY());
voidTag.setInteger("z", pos.getZ());
tileReplaced.readFromNBT(voidTag);
}
//Remove one item from stack
stack.shrink(1);
//Replace the block
world.setBlockState(pos, mimicBlockstate, 3);
//Make placing sound
SoundType soundtype = this.block.getSoundType();
world.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
//Replace the tile entity
TileEntity tile = world.getTileEntity(pos);
if (tile instanceof TileMimic)
{
TileMimic mimic = (TileMimic) tile;
mimic.tileTag = tileTag;
mimic.setReplacedState(replacedBlockstate);
mimic.setInventorySlotContents(0, replacedStack);
mimic.refreshTileEntity();
if (player.capabilities.isCreativeMode)
{
mimic.dropItemsOnBreak = false;
}
}
return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}
public boolean canReplaceTile(TileEntity tile)
{
if (tile instanceof TileEntityChest)
{
return true;
}
return tile == null;
}
public boolean canReplaceBlock(World world, BlockPos pos, IBlockState state) {
return state.getBlockHardness(world, pos) != -1.0F;
}
public NBTTagCompound getTagFromTileEntity(TileEntity tile)
{
NBTTagCompound tag = new NBTTagCompound();
if (tile != null)
{
return tile.writeToNBT(tag);
}
return tag;
}
@Override
public int getMetadata(int meta)
{
return meta;
}
}

View file

@ -6,10 +6,12 @@ import WayofTime.bloodmagic.core.RegistrarBloodMagicItems;
import WayofTime.bloodmagic.entity.mob.EntityMimic;
import WayofTime.bloodmagic.util.ChatUtil;
import WayofTime.bloodmagic.util.Utils;
import WayofTime.bloodmagic.util.StateUtil;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryHelper;
@ -39,8 +41,8 @@ public class TileMimic extends TileInventory implements ITickable {
public boolean dropItemsOnBreak = true;
public NBTTagCompound tileTag = new NBTTagCompound();
public TileEntity mimicedTile = null;
public int metaOfReplacedBlock = 0;
IBlockState stateOfReplacedBlock = Blocks.AIR.getDefaultState();
public int playerCheckRadius = 5;
public int potionSpawnRadius = 5;
public int potionSpawnInterval = 40;
@ -134,6 +136,13 @@ public class TileMimic extends TileInventory implements ITickable {
return false;
Utils.insertItemToTile(this, player, 0);
ItemStack stack = getStackInSlot(0);
if(stateOfReplacedBlock == Blocks.AIR.getDefaultState()) {
if (!stack.isEmpty() && stack.getItem() instanceof ItemBlock) {
Block block = ((ItemBlock) stack.getItem()).getBlock();
stateOfReplacedBlock = block.getDefaultState();
}
}
this.refreshTileEntity();
if (player.capabilities.isCreativeMode) {
@ -220,7 +229,7 @@ public class TileMimic extends TileInventory implements ITickable {
EntityMimic mimicEntity = new EntityMimic(getWorld());
mimicEntity.setPosition(pos.getX() + 0.5, pos.getY(), pos.getZ() + 0.5);
mimicEntity.initializeMimic(getStackInSlot(0), tileTag, dropItemsOnBreak, metaOfReplacedBlock, playerCheckRadius, pos);
mimicEntity.initializeMimic(getStackInSlot(0), tileTag, dropItemsOnBreak, stateOfReplacedBlock, playerCheckRadius, pos);
tileTag = null;
mimicedTile = null;
this.setInventorySlotContents(0, ItemStack.EMPTY);
@ -239,7 +248,7 @@ public class TileMimic extends TileInventory implements ITickable {
if (mimicedTile != null) {
dropMimicedTileInventory();
}
mimicedTile = getTileFromStackWithTag(getWorld(), pos, getStackInSlot(0), tileTag, metaOfReplacedBlock);
mimicedTile = getTileFromStackWithTag(getWorld(), pos, getStackInSlot(0), tileTag, stateOfReplacedBlock);
}
@Override
@ -248,8 +257,8 @@ public class TileMimic extends TileInventory implements ITickable {
dropItemsOnBreak = tag.getBoolean("dropItemsOnBreak");
tileTag = tag.getCompoundTag("tileTag");
metaOfReplacedBlock = tag.getInteger("metaOfReplacedBlock");
mimicedTile = getTileFromStackWithTag(getWorld(), pos, getStackInSlot(0), tileTag, metaOfReplacedBlock);
stateOfReplacedBlock = StateUtil.parseState(tag.getString("stateOfReplacedBlock"));
mimicedTile = getTileFromStackWithTag(getWorld(), pos, getStackInSlot(0), tileTag, stateOfReplacedBlock);
playerCheckRadius = tag.getInteger("playerCheckRadius");
potionSpawnRadius = tag.getInteger("potionSpawnRadius");
potionSpawnInterval = Math.max(1, tag.getInteger("potionSpawnInterval"));
@ -261,10 +270,10 @@ public class TileMimic extends TileInventory implements ITickable {
tag.setBoolean("dropItemsOnBreak", dropItemsOnBreak);
tag.setTag("tileTag", tileTag);
tag.setInteger("metaOfReplacedBlock", metaOfReplacedBlock);
tag.setInteger("playerCheckRadius", playerCheckRadius);
tag.setInteger("potionSpawnRadius", potionSpawnRadius);
tag.setInteger("potionSpawnInterval", potionSpawnInterval);
tag.setString("stateOfReplacedBlock",stateOfReplacedBlock.toString());
return tag;
}
@ -284,6 +293,14 @@ public class TileMimic extends TileInventory implements ITickable {
}
}
public IBlockState getReplacedState() {
return stateOfReplacedBlock;
}
public void setReplacedState(IBlockState state) {
stateOfReplacedBlock = state;
}
@Override
public boolean isItemValidForSlot(int slot, ItemStack itemstack) {
return slot == 0 && dropItemsOnBreak;
@ -293,13 +310,13 @@ public class TileMimic extends TileInventory implements ITickable {
World world = mimic.getWorld();
BlockPos pos = mimic.getPos();
replaceMimicWithBlockActual(world, pos, mimic.getStackInSlot(0), mimic.tileTag, mimic.metaOfReplacedBlock);
replaceMimicWithBlockActual(world, pos, mimic.getStackInSlot(0), mimic.tileTag, mimic.stateOfReplacedBlock);
}
public static boolean replaceMimicWithBlockActual(World world, BlockPos pos, ItemStack stack, NBTTagCompound tileTag, int replacedMeta) {
public static boolean replaceMimicWithBlockActual(World world, BlockPos pos, ItemStack stack, NBTTagCompound tileTag, IBlockState replacementState) {
if (!stack.isEmpty() && stack.getItem() instanceof ItemBlock) {
Block block = ((ItemBlock) stack.getItem()).getBlock();
IBlockState state = block.getStateFromMeta(replacedMeta);
IBlockState state = replacementState;
if (world.setBlockState(pos, state, 3)) {
TileEntity tile = world.getTileEntity(pos);
if (tile != null) {
@ -317,10 +334,10 @@ public class TileMimic extends TileInventory implements ITickable {
}
@Nullable
public static TileEntity getTileFromStackWithTag(World world, BlockPos pos, ItemStack stack, @Nullable NBTTagCompound tag, int replacementMeta) {
public static TileEntity getTileFromStackWithTag(World world, BlockPos pos, ItemStack stack, @Nullable NBTTagCompound tag, IBlockState replacementState) {
if (!stack.isEmpty() && stack.getItem() instanceof ItemBlock) {
Block block = ((ItemBlock) stack.getItem()).getBlock();
IBlockState state = block.getStateFromMeta(stack.getItemDamage());
IBlockState state = replacementState;
if (block.hasTileEntity(state)) {
TileEntity tile = block.createTileEntity(world, state);
@ -338,7 +355,7 @@ public class TileMimic extends TileInventory implements ITickable {
tile.setWorld(world);
try {
_blockMetadata.setInt(tile, replacementMeta);
_blockMetadata.setInt(tile, block.getMetaFromState(replacementState));
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
@ -349,4 +366,4 @@ public class TileMimic extends TileInventory implements ITickable {
return null;
}
}
}

View file

@ -0,0 +1,37 @@
package WayofTime.bloodmagic.util;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
public class StateUtil
{
public static IBlockState parseState(String blockInfo)
{
String[] split = blockInfo.split("\\[");
split[1] = split[1].substring(0, split[1].lastIndexOf("]")); // Make sure brackets are removed from state
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(split[0])); // Find the block
if (block == Blocks.AIR)
return Blocks.AIR.getDefaultState(); // The block is air, so we're looking at invalid data
BlockStateContainer blockState = block.getBlockState();
IBlockState returnState = blockState.getBaseState();
// Force our values into the state
String[] stateValues = split[1].split(","); // Splits up each value
for (String value : stateValues)
{
String[] valueSplit = value.split("="); // Separates property and value
IProperty property = blockState.getProperty(valueSplit[0]);
if (property != null)
returnState = returnState.withProperty(property, (Comparable) property.parseValue(valueSplit[1]).get()); // Force the property into the state
}
return returnState;
}
}