Fixed randomizer bag not working with quickreplace.
This commit is contained in:
@@ -23,6 +23,7 @@ public class QuickReplace {
|
||||
//and players will rarely switch between blocks that quickly.
|
||||
|
||||
private static Dictionary<UUID, IBlockState> blockStates = new Hashtable<>();
|
||||
private static Dictionary<UUID, ItemStack> itemStacks = new Hashtable<>();
|
||||
|
||||
public static void onBlockPlaced(BlockEvent.PlaceEvent event) {
|
||||
if (event.getWorld().isRemote) return;
|
||||
@@ -34,6 +35,7 @@ public class QuickReplace {
|
||||
//TODO base on player facing instead, no more messages (or break block clientside)
|
||||
|
||||
blockStates.put(event.getPlayer().getUniqueID(), event.getPlacedBlock());
|
||||
itemStacks.put(event.getPlayer().getUniqueID(), event.getPlayer().getHeldItem(event.getHand()));
|
||||
|
||||
//RayTraceResult result = event.getWorld().rayTraceBlocks(event.getPlayer().getPositionEyes(1f), event.getPlayer().getLookVec());
|
||||
EffortlessBuilding.packetHandler.sendTo(new QuickReplaceMessage(), (EntityPlayerMP) event.getPlayer());
|
||||
@@ -57,8 +59,8 @@ public class QuickReplace {
|
||||
}
|
||||
|
||||
IBlockState blockState = blockStates.get(player.getUniqueID());
|
||||
ItemStack itemStack = itemStacks.get(player.getUniqueID());
|
||||
|
||||
ItemStack itemStack = player.getHeldItem(player.swingingHand); //TODO check hand
|
||||
//SurvivalHelper.dropBlock(player.world, placedAgainstBlockPos, player);
|
||||
//player.world.setBlockState(placedAgainstBlockPos, blockState);
|
||||
SurvivalHelper.placeBlock(player.world, player, placedAgainstBlockPos, blockState, itemStack, message.getSideHit(), true, false);
|
||||
|
||||
@@ -9,6 +9,7 @@ import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumActionResult;
|
||||
@@ -17,7 +18,9 @@ import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.math.AxisAlignedBB;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.ItemHandlerHelper;
|
||||
import nl.requios.effortlessbuilding.item.ItemRandomizerBag;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
@@ -26,10 +29,27 @@ public class SurvivalHelper {
|
||||
|
||||
//From ItemBlock#onItemUse
|
||||
public static boolean placeBlock(World world, EntityPlayer player, BlockPos pos, IBlockState blockState, ItemStack itemstack, EnumFacing facing, boolean skipCollisionCheck, boolean playSound) {
|
||||
|
||||
//Randomizer bag synergy
|
||||
//Find itemstack that belongs to the blockstate
|
||||
if (itemstack.getItem() == EffortlessBuilding.ITEM_RANDOMIZER_BAG) {
|
||||
IItemHandler bagInventory = ItemRandomizerBag.getBagInventory(itemstack);
|
||||
itemstack = ItemRandomizerBag.findStack(bagInventory, Item.getItemFromBlock(blockState.getBlock()));
|
||||
}
|
||||
|
||||
//Check if itemstack is correct
|
||||
if (!(itemstack.getItem() instanceof ItemBlock) || Block.getBlockFromItem(itemstack.getItem()) != blockState.getBlock()) {
|
||||
EffortlessBuilding.log(player, "Cannot place block", true);
|
||||
EffortlessBuilding.log("SurvivalHelper#placeBlock: itemstack " + itemstack.toString() + " does not match blockstate " + blockState.toString());
|
||||
return false;
|
||||
}
|
||||
|
||||
Block block = ((ItemBlock) itemstack.getItem()).getBlock();
|
||||
|
||||
if (!itemstack.isEmpty() && canPlayerEdit(player, world, pos, itemstack) && mayPlace(world, block, pos, skipCollisionCheck, facing.getOpposite(), player))
|
||||
{
|
||||
//Drop existing block
|
||||
//TODO check if can replace
|
||||
dropBlock(world, pos, player);
|
||||
|
||||
//From ItemBlock#placeBlockAt
|
||||
@@ -60,10 +80,12 @@ public class SurvivalHelper {
|
||||
}
|
||||
|
||||
public static boolean canBreak(){
|
||||
//Can break using held tool? (or in creative)
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean canPlace(){
|
||||
public static boolean canReplace(){
|
||||
//Can be harvested with hand? (or in creative)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ import net.minecraftforge.common.util.BlockSnapshot;
|
||||
import net.minecraftforge.event.world.BlockEvent;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import net.minecraftforge.items.ItemStackHandler;
|
||||
import nl.requios.effortlessbuilding.Array;
|
||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||
import nl.requios.effortlessbuilding.Mirror;
|
||||
@@ -72,13 +73,9 @@ public class ItemRandomizerBag extends Item {
|
||||
pos = pos.offset(facing);
|
||||
}
|
||||
|
||||
// if (!player.isCreative()) {
|
||||
// toPlace.shrink(1);
|
||||
// }
|
||||
|
||||
IBlockState blockState = Block.getBlockFromItem(toPlace.getItem()).getStateForPlacement(world, pos, facing,
|
||||
hitX, hitY, hitZ, toPlace.getMetadata(), player, hand);
|
||||
//world.setBlockState(pos, blockState);
|
||||
|
||||
SurvivalHelper.placeBlock(world, player, pos, blockState, toPlace, facing, false, true);
|
||||
|
||||
//Synergy
|
||||
@@ -160,6 +157,16 @@ public class ItemRandomizerBag extends Item {
|
||||
return bagInventory.getStackInSlot(originalSlot);
|
||||
}
|
||||
|
||||
public static ItemStack findStack(IItemHandler bagInventory, Item item) {
|
||||
for (int i = 0; i < bagInventory.getSlots(); i++) {
|
||||
ItemStack stack = bagInventory.getStackInSlot(i);
|
||||
if (!stack.isEmpty() && stack.getItem() == item) {
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMaxItemUseDuration(ItemStack stack) {
|
||||
return 1;
|
||||
|
||||
Reference in New Issue
Block a user