Fix some more errors regarding blocks and Tile Entities and GUIs/Models.

GuiWidgetSlider now requires a forge event to work while being used to scroll with the mouse wheel.
This commit is contained in:
alexbegt committed 2019-05-22 19:34:05 -04:00
1 parent e0d244b5f6
commit a793b6b7bc
5 files changed
+84 -87

No files matched your search

@@ -3,22 +3,20 @@ package slimeknights.mantle.block;
import javax.annotation.Nonnull;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.state.BooleanProperty;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.state.StateContainer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.IWorld;
/**
/**
* Creates a block with textures that connect to other blocks
* <p>
* Based off a tutorial by Darkhax, used under the Creative Commons Zero 1.0 Universal license
*/
public class BlockConnectedTexture extends Block {
// These are the properties used for determining whether or not a side is connected. They
// do NOT take up block IDs, they are unlisted properties
public static final BooleanProperty CONNECTED_DOWN = BooleanProperty.create("connected_down");
@@ -27,46 +25,39 @@ public class BlockConnectedTexture extends Block {
public static final BooleanProperty CONNECTED_SOUTH = BooleanProperty.create("connected_south");
public static final BooleanProperty CONNECTED_WEST = BooleanProperty.create("connected_west");
public static final BooleanProperty CONNECTED_EAST = BooleanProperty.create("connected_east");
public BlockConnectedTexture(Material material) {
super(material);
public BlockConnectedTexture(Block.Properties builder) {
super(builder);
// By default none of the sides are connected
this.setDefaultState(this.blockState.getBaseState()
.withProperty(CONNECTED_DOWN, Boolean.FALSE)
.withProperty(CONNECTED_EAST, Boolean.FALSE)
.withProperty(CONNECTED_NORTH, Boolean.FALSE)
.withProperty(CONNECTED_SOUTH, Boolean.FALSE)
.withProperty(CONNECTED_UP, Boolean.FALSE)
.withProperty(CONNECTED_WEST, Boolean.FALSE));
}
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos position) {
// Creates the state to use for the block. This is where we check if every side is
// connectable or not.
return state.withProperty(CONNECTED_DOWN, this.isSideConnectable(state, world, position, EnumFacing.DOWN))
.withProperty(CONNECTED_EAST, this.isSideConnectable(state, world, position, EnumFacing.EAST))
.withProperty(CONNECTED_NORTH, this.isSideConnectable(state, world, position, EnumFacing.NORTH))
.withProperty(CONNECTED_SOUTH, this.isSideConnectable(state, world, position, EnumFacing.SOUTH))
.withProperty(CONNECTED_UP, this.isSideConnectable(state, world, position, EnumFacing.UP))
.withProperty(CONNECTED_WEST, this.isSideConnectable(state, world, position, EnumFacing.WEST));
this.setDefaultState(this.stateContainer.getBaseState()
.with(CONNECTED_DOWN, Boolean.FALSE)
.with(CONNECTED_EAST, Boolean.FALSE)
.with(CONNECTED_NORTH, Boolean.FALSE)
.with(CONNECTED_SOUTH, Boolean.FALSE)
.with(CONNECTED_UP, Boolean.FALSE)
.with(CONNECTED_WEST, Boolean.FALSE));
}
@Nonnull
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, new IProperty[] { CONNECTED_DOWN, CONNECTED_UP, CONNECTED_NORTH, CONNECTED_SOUTH, CONNECTED_WEST, CONNECTED_EAST });
public IBlockState updatePostPlacement(IBlockState stateIn, EnumFacing facing, IBlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
// Creates the state to use for the block. This is where we check if every side is
// connectable or not.
return stateIn.with(CONNECTED_DOWN, this.isSideConnectable(stateIn, worldIn, currentPos, EnumFacing.DOWN))
.with(CONNECTED_EAST, this.isSideConnectable(stateIn, worldIn, currentPos, EnumFacing.EAST))
.with(CONNECTED_NORTH, this.isSideConnectable(stateIn, worldIn, currentPos, EnumFacing.NORTH))
.with(CONNECTED_SOUTH, this.isSideConnectable(stateIn, worldIn, currentPos, EnumFacing.SOUTH))
.with(CONNECTED_UP, this.isSideConnectable(stateIn, worldIn, currentPos, EnumFacing.UP))
.with(CONNECTED_WEST, this.isSideConnectable(stateIn, worldIn, currentPos, EnumFacing.WEST));
}
// Since the block has state information but we are not switching the meta value, we have
// to override this method to return 0
@Override
public int getMetaFromState(IBlockState state) {
return 0;
protected void fillStateContainer(StateContainer.Builder<Block, IBlockState> builder) {
builder.add(CONNECTED_DOWN, CONNECTED_UP, CONNECTED_NORTH, CONNECTED_SOUTH, CONNECTED_WEST, CONNECTED_EAST);
}
/**
* Checks if a specific side of a block can connect to this block. For this example, a side
* is connectable if the block is the same block as this one.
@@ -77,14 +68,15 @@ public class BlockConnectedTexture extends Block {
* @param side The side of the block to check.
* @return Whether or not the side is connectable.
*/
private boolean isSideConnectable(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side) {
private boolean isSideConnectable(IBlockState state, IWorld world, BlockPos pos, EnumFacing side) {
final IBlockState connected = world.getBlockState(pos.offset(side));
return connected != null && canConnect(state, connected);
}
/**
* Checks if this block should connect to another block
* @param state BlockState to check
* @param original BlockState to check
* @param connected BlockState to check
* @return True if the block is valid to connect
*/
protected boolean canConnect(@Nonnull IBlockState original, @Nonnull IBlockState connected) {
@@ -1,5 +1,6 @@
package slimeknights.mantle.block;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
@@ -13,6 +14,7 @@ import net.minecraft.util.EnumBlockRenderType;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
@@ -22,8 +24,8 @@ import slimeknights.mantle.tileentity.TileInventory;
// Updated Version of InventoryBlock in Mantle
public abstract class BlockInventory extends BlockContainer {
protected BlockInventory(Material material) {
super(material);
protected BlockInventory(Block.Properties builder) {
super(builder);
}
// inventories usually need a tileEntity
@@ -34,7 +36,7 @@ public abstract class BlockInventory extends BlockContainer {
@Nonnull
@Override
public abstract TileEntity createNewTileEntity(@Nonnull World worldIn, int meta);
public abstract TileEntity createTileEntity(IBlockState state, IBlockReader world);
/**
* Called when the block is activated. Return true if a GUI is opened, false if the block has no GUI.
@@ -42,14 +44,13 @@ public abstract class BlockInventory extends BlockContainer {
protected abstract boolean openGui(EntityPlayer player, World world, BlockPos pos);
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side,
float clickX, float clickY, float clickZ) {
public boolean onBlockActivated(IBlockState state, World worldIn, BlockPos pos, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
if(player.isSneaking()) {
return false;
}
if(!world.isRemote) {
return this.openGui(player, world, pos);
if(!worldIn.isRemote) {
return this.openGui(player, worldIn, pos);
}
return true;
@@ -71,20 +72,17 @@ public abstract class BlockInventory extends BlockContainer {
}
@Override
public void breakBlock(World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state) {
TileEntity tileentity = worldIn.getTileEntity(pos);
public void onReplaced(IBlockState state, World worldIn, BlockPos pos, IBlockState newState, boolean isMoving) {
if (state.getBlock() != newState.getBlock()) {
super.onReplaced(state, worldIn, pos, newState, isMoving);
if(tileentity instanceof TileInventory) {
InventoryHelper.dropInventoryItems(worldIn, pos, (IInventory) tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
TileEntity tileentity = worldIn.getTileEntity(pos);
if(tileentity instanceof TileInventory) {
InventoryHelper.dropInventoryItems(worldIn, pos, (IInventory) tileentity);
worldIn.updateComparatorOutputLevel(pos, this);
}
}
super.breakBlock(worldIn, pos, state);
}
@Override
public int damageDropped(IBlockState state) {
return getMetaFromState(state);
}
// BlockContainer sets this to invisible
@@ -26,11 +26,6 @@ public class ModelHelper {
public static final TRSRTransformation BLOCK_THIRD_PERSON_RIGHT;
public static final TRSRTransformation BLOCK_THIRD_PERSON_LEFT;
public static TextureAtlasSprite getTextureFromBlock(Block block, int meta) {
IBlockState state = block.getStateFromMeta(meta);
return Minecraft.getInstance().getBlockRendererDispatcher().getBlockModelShapes().getTexture(state);
}
public static TextureAtlasSprite getTextureFromBlockstate(IBlockState state) {
return Minecraft.getInstance().getBlockRendererDispatcher().getBlockModelShapes().getTexture(state);
}
@@ -5,6 +5,8 @@ import net.minecraft.util.math.MathHelper;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
// a vertical slider!
@OnlyIn(Dist.CLIENT)
@@ -31,10 +33,12 @@ public class GuiWidgetSlider extends GuiWidget {
protected boolean isScrolling;
protected boolean isHighlighted;
// where the slider was clicked on the slider itself (not on the bar, on the thing that slides)
private int clickX;
private int clickY;
private boolean clickedBar; // if the bar has already been clicked and not released
private boolean useMouseWheel;
public GuiWidgetSlider(GuiElement slider, GuiElement sliderHighlighted, GuiElement sliderDisabled, GuiElement slideBarTop, GuiElement slideBarBottom, GuiElementScalable slideBar) {
this.slider = slider;
@@ -57,6 +61,7 @@ public class GuiWidgetSlider extends GuiWidget {
enabled = true;
hidden = false;
useMouseWheel = false;
}
/** Sets the height of the whole slider and slidebar */
@@ -150,18 +155,7 @@ public class GuiWidgetSlider extends GuiWidget {
}
boolean mouseDown = Minecraft.getInstance().mouseHelper.isLeftDown(); // left mouse button
int wheel = Mouse.getDWheel();
if(useMouseWheel) {
if(wheel > 0) {
decrement();
return;
}
else if(wheel < 0) {
increment();
return;
}
}
this.useMouseWheel = useMouseWheel;
// relative position inside the widget
int x = mouseX - xPos;
@@ -223,6 +217,26 @@ public class GuiWidgetSlider extends GuiWidget {
}
}
@SubscribeEvent
public void onGuiMouseEvent(GuiScreenEvent.MouseScrollEvent.Pre event) {
double dWheel = event.getScrollDelta();
double mouseX = event.getMouseX();
double mouseY = event.getMouseY();
if(useMouseWheel) {
if(dWheel > 0.0) {
decrement();
event.setCanceled(true);
return;
}
else if(dWheel < 0.0) {
increment();
event.setCanceled(true);
return;
}
}
}
public int increment() {
setSliderValue(currentValue + increment);
return currentValue;
@@ -14,6 +14,7 @@ import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.wrapper.InvWrapper;
@@ -31,6 +32,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
protected boolean hasCustomName;
protected int stackSizeLimit;
protected IItemHandlerModifiable itemHandler;
protected LazyOptional<IItemHandlerModifiable> itemHandlerCap;
/**
* @param name Localization String for the inventory title. Can be overridden through setCustomName
@@ -48,18 +50,14 @@ public class TileInventory extends MantleTileEntity implements IInventory {
this.stackSizeLimit = maxStackSize;
this.inventoryTitle = name;
this.itemHandler = new InvWrapper(this);
}
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, EnumFacing facing) {
return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
this.itemHandlerCap = LazyOptional.of(() -> this.itemHandler);
}
@SuppressWarnings("unchecked")
@Override
public <T> net.minecraftforge.common.util.LazyOptional<T> getCapability(@Nonnull Capability<T> capability, EnumFacing facing) {
if(capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
return itemHandler.cast();
return itemHandlerCap.cast();
}
return super.getCapability(capability, facing);
}
@@ -150,7 +148,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
}
// split itemstack
itemStack = itemStack.splitStack(quantity);
itemStack = itemStack.split(quantity);
// slot is empty, set to ItemStack.EMPTY
// isn't this redundant to the above check?
if(getStackInSlot(slot).getCount() == 0) {
@@ -252,7 +250,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
readInventoryFromNBT(tags);
if(tags.contains("CustomName", 8)) {
this.inventoryTitle = tags.getString("CustomName");
this.inventoryTitle = ITextComponent.Serializer.fromJson(tags.getString("CustomName"));
}
}
@@ -266,7 +264,7 @@ public class TileInventory extends MantleTileEntity implements IInventory {
writeInventoryToNBT(tags);
if(this.hasCustomName()) {
tags.putString("CustomName", this.inventoryTitle);
tags.putString("CustomName", ITextComponent.Serializer.toJson(this.inventoryTitle));
}
return tags;
}