Added compatibility for a few block proxies, like /dank/null.
This commit is contained in:
@@ -15,6 +15,7 @@ import net.minecraft.util.math.Vec3d;
|
|||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.event.world.BlockEvent;
|
import net.minecraftforge.event.world.BlockEvent;
|
||||||
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||||
|
import nl.requios.effortlessbuilding.helper.CompatHelper;
|
||||||
import nl.requios.effortlessbuilding.helper.ReachHelper;
|
import nl.requios.effortlessbuilding.helper.ReachHelper;
|
||||||
import nl.requios.effortlessbuilding.helper.SurvivalHelper;
|
import nl.requios.effortlessbuilding.helper.SurvivalHelper;
|
||||||
import nl.requios.effortlessbuilding.item.ItemRandomizerBag;
|
import nl.requios.effortlessbuilding.item.ItemRandomizerBag;
|
||||||
@@ -111,21 +112,21 @@ public class BuildModifiers {
|
|||||||
|
|
||||||
//Get itemstack
|
//Get itemstack
|
||||||
ItemStack itemStack = player.getHeldItem(EnumHand.MAIN_HAND);
|
ItemStack itemStack = player.getHeldItem(EnumHand.MAIN_HAND);
|
||||||
if (itemStack.isEmpty() || !(itemStack.getItem() instanceof ItemBlock || itemStack.getItem() instanceof ItemRandomizerBag)) {
|
if (itemStack.isEmpty() || !CompatHelper.isItemBlockProxy(itemStack)) {
|
||||||
itemStack = player.getHeldItem(EnumHand.OFF_HAND);
|
itemStack = player.getHeldItem(EnumHand.OFF_HAND);
|
||||||
}
|
}
|
||||||
if (itemStack.isEmpty() || !(itemStack.getItem() instanceof ItemBlock || itemStack.getItem() instanceof ItemRandomizerBag)) {
|
if (itemStack.isEmpty() || !CompatHelper.isItemBlockProxy(itemStack)) {
|
||||||
return blockStates;
|
return blockStates;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Get ItemBlock stack
|
//Get ItemBlock stack
|
||||||
ItemStack itemBlock = ItemStack.EMPTY;
|
ItemStack itemBlock = ItemStack.EMPTY;
|
||||||
if (itemStack.getItem() instanceof ItemBlock) itemBlock = itemStack;
|
if (itemStack.getItem() instanceof ItemBlock) itemBlock = itemStack;
|
||||||
ItemRandomizerBag.resetRandomness();
|
else itemBlock = CompatHelper.getItemBlockFromStack(itemStack);
|
||||||
|
|
||||||
//Add blocks in posList first
|
//Add blocks in posList first
|
||||||
for (BlockPos blockPos : posList) {
|
for (BlockPos blockPos : posList) {
|
||||||
if (itemStack.getItem() instanceof ItemRandomizerBag) itemBlock = ItemRandomizerBag.pickRandomStack(ItemRandomizerBag.getBagInventory(itemStack));
|
//if (itemStack.getItem() instanceof ItemRandomizerBag) itemBlock = ItemRandomizerBag.pickRandomStack(ItemRandomizerBag.getBagInventory(itemStack));
|
||||||
IBlockState blockState = getBlockStateFromItem(itemBlock, player, blockPos, facing, hitVec, EnumHand.MAIN_HAND);
|
IBlockState blockState = getBlockStateFromItem(itemBlock, player, blockPos, facing, hitVec, EnumHand.MAIN_HAND);
|
||||||
blockStates.add(blockState);
|
blockStates.add(blockState);
|
||||||
itemStacks.add(itemBlock);
|
itemStacks.add(itemBlock);
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package nl.requios.effortlessbuilding.helper;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import net.minecraft.block.state.IBlockState;
|
||||||
|
import net.minecraft.client.Minecraft;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemBlock;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.util.ResourceLocation;
|
||||||
|
import net.minecraftforge.fml.common.Loader;
|
||||||
|
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||||
|
import net.minecraftforge.items.CapabilityItemHandler;
|
||||||
|
import net.minecraftforge.items.IItemHandler;
|
||||||
|
import nl.requios.effortlessbuilding.EffortlessBuilding;
|
||||||
|
import nl.requios.effortlessbuilding.item.ItemRandomizerBag;
|
||||||
|
|
||||||
|
public class CompatHelper {
|
||||||
|
// Get a handle to the dank null item instance. This will remain null if the mod doesn't load
|
||||||
|
// and all checks will fail, so it works.
|
||||||
|
@GameRegistry.ObjectHolder("danknull:dank_null")
|
||||||
|
public static final Item dankNullItem = null;
|
||||||
|
|
||||||
|
@GameRegistry.ObjectHolder("xreliquary:void_tear")
|
||||||
|
public static final Item voidTearItem = null;
|
||||||
|
|
||||||
|
// Check if the item given is a proxy for blocks. For now, we check for the randomizer bag,
|
||||||
|
// /dank/null, or plain old blocks.
|
||||||
|
public static boolean isItemBlockProxy(ItemStack stack) {
|
||||||
|
Item item = stack.getItem();
|
||||||
|
if(item instanceof ItemBlock)
|
||||||
|
return true;
|
||||||
|
if((item instanceof ItemRandomizerBag))
|
||||||
|
return true;
|
||||||
|
if((item == dankNullItem) || (item == voidTearItem))
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the block to be placed by this proxy. For the /dank/null, it's the slot stack
|
||||||
|
// pointed to by nbt integer selectedIndex.
|
||||||
|
public static ItemStack getItemBlockFromStack(ItemStack stack) {
|
||||||
|
Item item = stack.getItem();
|
||||||
|
if(item instanceof ItemRandomizerBag) {
|
||||||
|
ItemRandomizerBag.resetRandomness();
|
||||||
|
return ItemRandomizerBag.pickRandomStack(ItemRandomizerBag.getBagInventory(stack));
|
||||||
|
} else if(item == dankNullItem) {
|
||||||
|
int index = 0;
|
||||||
|
if(stack.hasTagCompound() && stack.getTagCompound().hasKey("selectedIndex"))
|
||||||
|
index = stack.getTagCompound().getInteger("selectedIndex");
|
||||||
|
return stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).getStackInSlot(index);
|
||||||
|
}
|
||||||
|
return ItemStack.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack getItemBlockByState(ItemStack stack, IBlockState state) {
|
||||||
|
Item blockItem = Item.getItemFromBlock(state.getBlock());
|
||||||
|
if(stack.getItem() instanceof ItemBlock)
|
||||||
|
return stack;
|
||||||
|
else if(stack.getItem() instanceof ItemRandomizerBag) {
|
||||||
|
IItemHandler bagInventory = ItemRandomizerBag.getBagInventory(stack);
|
||||||
|
return ItemRandomizerBag.findStack(bagInventory, blockItem);
|
||||||
|
} else if(stack.getItem() == dankNullItem) {
|
||||||
|
int index = itemHandlerSlotForItem(stack, blockItem);
|
||||||
|
if(index >= 0)
|
||||||
|
return stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).getStackInSlot(index);
|
||||||
|
}
|
||||||
|
return ItemStack.EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle IItemHandler slot stacks not being modifiable. We must call IItemHandler#extractItem,
|
||||||
|
// because the ItemStack returned by IItemHandler#getStackInSlot isn't modifiable.
|
||||||
|
public static void shrinkStack(ItemStack origStack, ItemStack curStack) {
|
||||||
|
if(origStack.getItem() == dankNullItem) {
|
||||||
|
int index = itemHandlerSlotForItem(origStack, curStack.getItem());
|
||||||
|
origStack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null).extractItem(index, 1, false);
|
||||||
|
} else
|
||||||
|
curStack.shrink(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int itemHandlerSlotForItem(ItemStack stack, Item blockItem) {
|
||||||
|
IItemHandler handler = stack.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
|
||||||
|
for(int i = 0; i < handler.getSlots(); i++) {
|
||||||
|
ItemStack ref = handler.getStackInSlot(i);
|
||||||
|
if(ref.getItem() instanceof ItemBlock)
|
||||||
|
if(ref.getItem() == blockItem)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,16 +34,17 @@ public class SurvivalHelper {
|
|||||||
//Used for all placing of blocks in this mod.
|
//Used for all placing of blocks in this mod.
|
||||||
//Checks if area is loaded, if player has the right permissions, if existing block can be replaced (drops it if so) and consumes an item from the stack.
|
//Checks if area is loaded, if player has the right permissions, if existing block can be replaced (drops it if so) and consumes an item from the stack.
|
||||||
//Based on ItemBlock#onItemUse
|
//Based on ItemBlock#onItemUse
|
||||||
public static boolean placeBlock(World world, EntityPlayer player, BlockPos pos, IBlockState blockState, ItemStack itemstack, EnumFacing facing, boolean skipCollisionCheck, boolean playSound) {
|
public static boolean placeBlock(World world, EntityPlayer player, BlockPos pos, IBlockState blockState, ItemStack origstack, EnumFacing facing, boolean skipCollisionCheck, boolean playSound) {
|
||||||
if (!world.isBlockLoaded(pos, true)) return false;
|
if (!world.isBlockLoaded(pos, true)) return false;
|
||||||
|
ItemStack itemstack = origstack;
|
||||||
|
|
||||||
//Randomizer bag synergy
|
//Randomizer bag, other proxy item synergy
|
||||||
//Find itemstack that belongs to the blockstate
|
//Preliminary compatibility code for other items that hold blocks
|
||||||
if (itemstack.getItem() == EffortlessBuilding.ITEM_RANDOMIZER_BAG) {
|
if(CompatHelper.isItemBlockProxy(itemstack))
|
||||||
IItemHandler bagInventory = ItemRandomizerBag.getBagInventory(itemstack);
|
itemstack = CompatHelper.getItemBlockByState(itemstack, blockState);
|
||||||
itemstack = ItemRandomizerBag.findStack(bagInventory, Item.getItemFromBlock(blockState.getBlock()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if(!(itemstack.getItem() instanceof ItemBlock))
|
||||||
|
return false;
|
||||||
Block block = ((ItemBlock) itemstack.getItem()).getBlock();
|
Block block = ((ItemBlock) itemstack.getItem()).getBlock();
|
||||||
|
|
||||||
if (canPlace(world, player, pos, blockState, itemstack, skipCollisionCheck, facing.getOpposite())) {
|
if (canPlace(world, player, pos, blockState, itemstack, skipCollisionCheck, facing.getOpposite())) {
|
||||||
@@ -70,7 +71,8 @@ public class SurvivalHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!player.isCreative() && Block.getBlockFromItem(itemstack.getItem()) == block) {
|
if (!player.isCreative() && Block.getBlockFromItem(itemstack.getItem()) == block) {
|
||||||
itemstack.shrink(1);
|
//itemstack.shrink(1);
|
||||||
|
CompatHelper.shrinkStack(origstack, itemstack);
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ import nl.requios.effortlessbuilding.buildmode.ModeSettingsManager;
|
|||||||
import nl.requios.effortlessbuilding.buildmodifier.ModifierSettingsManager;
|
import nl.requios.effortlessbuilding.buildmodifier.ModifierSettingsManager;
|
||||||
import nl.requios.effortlessbuilding.gui.buildmode.RadialMenu;
|
import nl.requios.effortlessbuilding.gui.buildmode.RadialMenu;
|
||||||
import nl.requios.effortlessbuilding.gui.buildmodifier.ModifierSettingsGui;
|
import nl.requios.effortlessbuilding.gui.buildmodifier.ModifierSettingsGui;
|
||||||
|
import nl.requios.effortlessbuilding.helper.CompatHelper;
|
||||||
import nl.requios.effortlessbuilding.helper.ReachHelper;
|
import nl.requios.effortlessbuilding.helper.ReachHelper;
|
||||||
import nl.requios.effortlessbuilding.render.RenderHandler;
|
import nl.requios.effortlessbuilding.render.RenderHandler;
|
||||||
import nl.requios.effortlessbuilding.render.ShaderHandler;
|
import nl.requios.effortlessbuilding.render.ShaderHandler;
|
||||||
@@ -167,8 +168,7 @@ public class ClientProxy implements IProxy {
|
|||||||
//KeyBinding.setKeyBindState(mc.gameSettings.keyBindUseItem.getKeyCode(), false);
|
//KeyBinding.setKeyBindState(mc.gameSettings.keyBindUseItem.getKeyCode(), false);
|
||||||
|
|
||||||
ItemStack currentItemStack = player.getHeldItem(EnumHand.MAIN_HAND);
|
ItemStack currentItemStack = player.getHeldItem(EnumHand.MAIN_HAND);
|
||||||
if (currentItemStack.getItem() instanceof ItemBlock ||
|
if (CompatHelper.isItemBlockProxy(currentItemStack)) {
|
||||||
(currentItemStack.getItem() instanceof ItemRandomizerBag && !player.isSneaking())) {
|
|
||||||
|
|
||||||
//find position in distance
|
//find position in distance
|
||||||
RayTraceResult lookingAt = getLookingAt(player);
|
RayTraceResult lookingAt = getLookingAt(player);
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import nl.requios.effortlessbuilding.buildmode.IBuildMode;
|
|||||||
import nl.requios.effortlessbuilding.buildmode.ModeSettingsManager;
|
import nl.requios.effortlessbuilding.buildmode.ModeSettingsManager;
|
||||||
import nl.requios.effortlessbuilding.buildmodifier.BuildModifiers;
|
import nl.requios.effortlessbuilding.buildmodifier.BuildModifiers;
|
||||||
import nl.requios.effortlessbuilding.buildmodifier.ModifierSettingsManager;
|
import nl.requios.effortlessbuilding.buildmodifier.ModifierSettingsManager;
|
||||||
|
import nl.requios.effortlessbuilding.helper.CompatHelper;
|
||||||
import nl.requios.effortlessbuilding.helper.ReachHelper;
|
import nl.requios.effortlessbuilding.helper.ReachHelper;
|
||||||
import nl.requios.effortlessbuilding.helper.SurvivalHelper;
|
import nl.requios.effortlessbuilding.helper.SurvivalHelper;
|
||||||
import nl.requios.effortlessbuilding.item.ItemRandomizerBag;
|
import nl.requios.effortlessbuilding.item.ItemRandomizerBag;
|
||||||
@@ -90,7 +91,7 @@ public class BlockPreviewRenderer {
|
|||||||
if (modeSettings.getBuildMode() == BuildModes.BuildModeEnum.Normal) lookingAt = Minecraft.getMinecraft().objectMouseOver;
|
if (modeSettings.getBuildMode() == BuildModes.BuildModeEnum.Normal) lookingAt = Minecraft.getMinecraft().objectMouseOver;
|
||||||
|
|
||||||
ItemStack mainhand = player.getHeldItemMainhand();
|
ItemStack mainhand = player.getHeldItemMainhand();
|
||||||
boolean toolInHand = !(!mainhand.isEmpty() && (mainhand.getItem() instanceof ItemBlock || mainhand.getItem() instanceof ItemRandomizerBag));
|
boolean toolInHand = !(!mainhand.isEmpty() && CompatHelper.isItemBlockProxy(mainhand));
|
||||||
|
|
||||||
BlockPos startPos = null;
|
BlockPos startPos = null;
|
||||||
EnumFacing sideHit = null;
|
EnumFacing sideHit = null;
|
||||||
@@ -223,6 +224,8 @@ public class BlockPreviewRenderer {
|
|||||||
BlockPos blockPos = coordinates.get(i);
|
BlockPos blockPos = coordinates.get(i);
|
||||||
IBlockState blockState = blockStates.get(i);
|
IBlockState blockState = blockStates.get(i);
|
||||||
ItemStack itemstack = itemStacks.get(i);
|
ItemStack itemstack = itemStacks.get(i);
|
||||||
|
if(CompatHelper.isItemBlockProxy(itemstack))
|
||||||
|
itemstack = CompatHelper.getItemBlockByState(itemstack, blockState);
|
||||||
|
|
||||||
//Check if can place
|
//Check if can place
|
||||||
//If check is turned off, check if blockstate is the same (for dissolve effect)
|
//If check is turned off, check if blockstate is the same (for dissolve effect)
|
||||||
|
|||||||
Reference in New Issue
Block a user