Class CustomBlock
public abstract class CustomBlock
extends java.lang.Object
BlockManager.registerCustomBlock(CustomBlock)
.
There should be only one instance of this class for each custom block type, every individual blocks will execute the callbacks present there. Each of which contains the custom block position and the instance concerned.
Each block in a chunk contains 2 id, the block state id (only visual) and a custom block id corresponding to
getCustomBlockId()
. A custom block is responsible for the blocks tick, the custom break time feature,
and some useful callbacks.
-
Field Summary
Fields Modifier and Type Field Description static byte
MAX_STAGE
-
Constructor Summary
Constructors Constructor Description CustomBlock(short defaultBlockStateId, java.lang.String identifier)
CustomBlock(Block block, java.lang.String identifier)
-
Method Summary
Modifier and Type Method Description Data
createData(Instance instance, BlockPosition blockPosition, Data data)
Initialises data for this block.boolean
enableCustomBreakDelay()
Used to enable the custom break delay fromgetBreakDelay(Player, BlockPosition, byte, Set)
Disabling it would result in having vanilla timeboolean
enableMultiPlayerBreaking()
Gets if this block breaking time can be reduced by having multiple players digging it.int
getBreakDelay(Player player, BlockPosition position, byte stage, java.util.Set<Player> breakers)
Called when the player requests the next stage break delayint
getBreakEntityId(Player firstBreaker)
java.util.Set<Player>
getBreakers(Instance instance, BlockPosition blockPosition)
Gets all the breakers of a block, only ifenableMultiPlayerBreaking()
is enabled.byte
getBreakStage(Instance instance, BlockPosition blockPosition)
Gets the block break stage at a position, only work ifenableMultiPlayerBreaking()
is enabled.abstract short
getCustomBlockId()
This id can be serialized in chunk file, meaning no duplicate should exist Changing this value halfway should mean potentially breaking the worldshort
getDefaultBlockStateId()
This is the default block state id when the custom block is set, it is possible to change this value per block usingBlockModifier.setSeparateBlocks(int, int, int, short, short)
float
getDrag(Instance instance, BlockPosition blockPosition)
Gets the drag of this block.java.lang.String
getIdentifier()
The custom block identifier, used to retrieve the custom block object withBlockManager.getCustomBlock(String)
and to set custom block in the instance.LootTable
getLootTable(LootTableManager tableManager)
Returns the loot table associated to this block.UpdateOption
getUpdateOption()
The update option is used to define the delay between twoupdate(Instance, BlockPosition, Data)
execution.void
handleContact(Instance instance, BlockPosition position, Entity touching)
Defines custom behaviour for entities touching this block.boolean
hasUpdate()
Gets if thisCustomBlock
requires any tick update.abstract void
onDestroy(Instance instance, BlockPosition blockPosition, Data data)
Called when a custom block has been destroyed or replaced.boolean
onExplode(Instance instance, BlockPosition position, Data lootTableArguments)
Called when an explosion wants to destroy this block.abstract boolean
onInteract(Player player, Player.Hand hand, BlockPosition blockPosition, Data data)
Handles interactions with this block.abstract void
onPlace(Instance instance, BlockPosition blockPosition, Data data)
Called when a custom block has been placed.boolean
processStage(Instance instance, BlockPosition blockPosition, Player player, byte stageIncrease)
Process one stage on the block, break it if it excessMAX_STAGE
, only ifenableMultiPlayerBreaking()
is enabled.void
removeDiggingInformation(Instance instance, BlockPosition blockPosition)
void
scheduledUpdate(Instance instance, BlockPosition position, Data blockData)
Called when a scheduled update on this block happens.void
startDigging(Instance instance, BlockPosition blockPosition, Player player)
Called when a player start digging this custom block, process all necessary data ifenableMultiPlayerBreaking()
is enabled.void
stopDigging(Instance instance, BlockPosition blockPosition, Player player)
Called when a player stop digging a block, does remove the block break animation if he was the only breaker.void
update(Instance instance, BlockPosition blockPosition, Data data)
Calling delay depends ongetUpdateOption()
which should be overridden.void
updateFromNeighbor(Instance instance, BlockPosition thisPosition, BlockPosition neighborPosition, boolean directNeighbor)
Updates this block from a neighbor.void
writeBlockEntity(BlockPosition position, Data blockData, org.jglrxavpok.hephaistos.nbt.NBTCompound nbt)
Allows custom block to write block entity data to a given NBT compound.
-
Field Details
-
MAX_STAGE
public static final byte MAX_STAGE- See Also:
- Constant Field Values
-
-
Constructor Details
-
CustomBlock
public CustomBlock(short defaultBlockStateId, @NotNull java.lang.String identifier)- Parameters:
defaultBlockStateId
- the default block state ididentifier
- the custom block identifier
-
CustomBlock
-
-
Method Details
-
getBreakEntityId
-
update
public void update(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data)Calling delay depends ongetUpdateOption()
which should be overridden.- Parameters:
instance
- the instance of the blockblockPosition
- the position of the blockdata
- the data associated with the block- Throws:
java.lang.UnsupportedOperationException
- ifgetUpdateOption()
is not null but the update method is not overridden
-
getUpdateOption
The update option is used to define the delay between twoupdate(Instance, BlockPosition, Data)
execution.If this is not null,
update(Instance, BlockPosition, Data)
should be overridden or errors with occurs.- Returns:
- the update option of the block, null if not any
-
onPlace
public abstract void onPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data)Called when a custom block has been placed.- Parameters:
instance
- the instance of the blockblockPosition
- the position of the blockdata
- the data associated with the block
-
onDestroy
public abstract void onDestroy(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data)Called when a custom block has been destroyed or replaced.- Parameters:
instance
- the instance of the blockblockPosition
- the position of the blockdata
- the data associated with the block
-
onInteract
public abstract boolean onInteract(@NotNull Player player, @NotNull Player.Hand hand, @NotNull BlockPosition blockPosition, @Nullable Data data)Handles interactions with this block. Can also block normal item use (containers should block when opening the menu, this prevents the player from placing a block when opening it for instance).- Parameters:
player
- the player interactinghand
- the hand used to interactblockPosition
- the position of this blockdata
- the data at this position- Returns:
- true if this block blocks normal item use, false otherwise
-
getCustomBlockId
public abstract short getCustomBlockId()This id can be serialized in chunk file, meaning no duplicate should exist Changing this value halfway should mean potentially breaking the world- Returns:
- the custom block id
-
getBreakDelay
public int getBreakDelay(@NotNull Player player, @NotNull BlockPosition position, byte stage, java.util.Set<Player> breakers)Called when the player requests the next stage break delay- Parameters:
player
- the player who is trying to break the blockposition
- the block positionstage
- the current break stage of the block (0-10)breakers
- the list containing all the players currently digging this block- Returns:
- the time in tick to pass to the next state, 0 to instant break it. negative value allow to skip stages (-2 will skip 2 stages per tick)
- See Also:
to enable/disable it
-
enableCustomBreakDelay
public boolean enableCustomBreakDelay()Used to enable the custom break delay fromgetBreakDelay(Player, BlockPosition, byte, Set)
Disabling it would result in having vanilla time- Returns:
- true to enable custom break delay
-
enableMultiPlayerBreaking
public boolean enableMultiPlayerBreaking()Gets if this block breaking time can be reduced by having multiple players digging it.WARNING: this should be constant, do not change this value halfway.
- Returns:
- true to enable the multi-player breaking feature
-
hasUpdate
public boolean hasUpdate()Gets if thisCustomBlock
requires any tick update.- Returns:
- true if
getUpdateOption()
is not null and the value is positive
-
handleContact
public void handleContact(@NotNull Instance instance, @NotNull BlockPosition position, @NotNull Entity touching)Defines custom behaviour for entities touching this block.- Parameters:
instance
- the instanceposition
- the position at which the block istouching
- the entity currently touching the block
-
getDefaultBlockStateId
public short getDefaultBlockStateId()This is the default block state id when the custom block is set, it is possible to change this value per block usingBlockModifier.setSeparateBlocks(int, int, int, short, short)
Meaning that you should not believe that your custom blocks id will always be this one.
- Returns:
- the default visual block id
-
getIdentifier
@NotNull public java.lang.String getIdentifier()The custom block identifier, used to retrieve the custom block object withBlockManager.getCustomBlock(String)
and to set custom block in the instance.- Returns:
- the custom block identifier
-
createData
@Nullable public Data createData(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @Nullable Data data)Initialises data for this block.- Parameters:
blockPosition
- the position of the targeted blockdata
- data given to 'setBlock', can be null- Returns:
- Data for this block. Can be null, 'data', or a new object
-
updateFromNeighbor
public void updateFromNeighbor(@NotNull Instance instance, @NotNull BlockPosition thisPosition, @NotNull BlockPosition neighborPosition, boolean directNeighbor)Updates this block from a neighbor. By default calls 'update' if directNeighbor is true.- Parameters:
instance
- current instancethisPosition
- this block's positionneighborPosition
- the neighboring block which triggered the updatedirectNeighbor
- is the neighbor directly connected to this block? (No diagonals)
-
scheduledUpdate
public void scheduledUpdate(@NotNull Instance instance, @NotNull BlockPosition position, @Nullable Data blockData)Called when a scheduled update on this block happens. By default, calls 'update'.- Parameters:
instance
- the instance of the blockposition
- the position of the blockblockData
- the data of the block
-
getDrag
Gets the drag of this block.It has to be between 0 and 1.
- Returns:
- the drag of this block
-
writeBlockEntity
public void writeBlockEntity(@NotNull BlockPosition position, @Nullable Data blockData, @NotNull org.jglrxavpok.hephaistos.nbt.NBTCompound nbt)Allows custom block to write block entity data to a given NBT compound. Used to send block entity data to the client over the network. Can also be used to save block entity data on disk for compatible chunk savers.- Parameters:
position
- position of the blockblockData
- equivalent toinstance.getBlockData(position)
nbt
- the nbt to write in theChunkDataPacket
-
onExplode
public boolean onExplode(@NotNull Instance instance, @NotNull BlockPosition position, Data lootTableArguments)Called when an explosion wants to destroy this block.- Parameters:
instance
- the instancelootTableArguments
- arguments used in the loot table loot generation- Returns:
- 'true' if the explosion should happen on this block, 'false' to cancel the destruction. Returning true does NOT block the explosion rays, ie it does not change the block explosion resistance
-
getLootTable
Returns the loot table associated to this block. Return null to use vanilla behavior.- Parameters:
tableManager
- the loot table manager- Returns:
- the loot table associated to this block
-
startDigging
public void startDigging(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Player player)Called when a player start digging this custom block, process all necessary data ifenableMultiPlayerBreaking()
is enabled.- Parameters:
instance
- the instance of the blockblockPosition
- the position of the blockplayer
- the player who started digging
-
stopDigging
public void stopDigging(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Player player)Called when a player stop digging a block, does remove the block break animation if he was the only breaker.- Parameters:
instance
- the instance of the blockblockPosition
- the position of the blockplayer
- the player who stopped digging
-
processStage
public boolean processStage(@NotNull Instance instance, @NotNull BlockPosition blockPosition, @NotNull Player player, byte stageIncrease)Process one stage on the block, break it if it excessMAX_STAGE
, only ifenableMultiPlayerBreaking()
is enabled.- Parameters:
instance
- the instance of the blockblockPosition
- the position of the blockplayer
- the player who processed one stage on the blockstageIncrease
- the number of stage increase- Returns:
- true if the block can continue being digged
- Throws:
java.lang.IllegalStateException
- ifenableMultiPlayerBreaking()
is disabled
-
removeDiggingInformation
public void removeDiggingInformation(@NotNull Instance instance, @NotNull BlockPosition blockPosition) -
getBreakers
@Nullable public java.util.Set<Player> getBreakers(@NotNull Instance instance, @NotNull BlockPosition blockPosition)Gets all the breakers of a block, only ifenableMultiPlayerBreaking()
is enabled.- Parameters:
instance
- the instance of the blockblockPosition
- the position of the block- Returns:
- the
Set
of breakers of a block - Throws:
java.lang.IllegalStateException
- ifenableMultiPlayerBreaking()
is disabled
-
getBreakStage
Gets the block break stage at a position, only work ifenableMultiPlayerBreaking()
is enabled.- Parameters:
instance
- the instance of the custom blockblockPosition
- the position of the custom block- Returns:
- the break stage at the position. Can also be 0 when nonexistent
-