mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-21 06:41:43 +01:00
Replaced blockId by blockStateId
This commit is contained in:
parent
3e7ce7c1a0
commit
67db1d6568
File diff suppressed because it is too large
Load Diff
@ -113,7 +113,7 @@ public class PlayerInit {
|
||||
|
||||
connectionManager.addPlayerInitialization(player -> {
|
||||
player.addEventCallback(EntityAttackEvent.class, event -> {
|
||||
Entity entity = event.getTarget();
|
||||
final Entity entity = event.getTarget();
|
||||
if (entity instanceof EntityCreature) {
|
||||
EntityCreature creature = (EntityCreature) entity;
|
||||
creature.damage(DamageType.fromPlayer(player), -1);
|
||||
@ -135,10 +135,13 @@ public class PlayerInit {
|
||||
if (event.getHand() != Player.Hand.MAIN)
|
||||
return;
|
||||
|
||||
if (event.getBlockId() == Block.STONE.getBlockId()) {
|
||||
final Block block = Block.fromStateId(event.getBlockStateId());
|
||||
|
||||
if (block == Block.STONE) {
|
||||
event.setCustomBlock((short) 2); // custom stone block
|
||||
System.out.println("custom stone");
|
||||
}
|
||||
if (event.getBlockId() == Block.TORCH.getBlockId()) {
|
||||
if (block == Block.TORCH) {
|
||||
event.setCustomBlock((short) 3); // custom torch block
|
||||
}
|
||||
|
||||
@ -147,8 +150,11 @@ public class PlayerInit {
|
||||
p.teleport(player.getPosition());
|
||||
}*/
|
||||
|
||||
ChickenCreature chickenCreature = new ChickenCreature(player.getPosition());
|
||||
chickenCreature.setInstance(player.getInstance());
|
||||
for (int i = 0; i < 1; i++) {
|
||||
ChickenCreature chickenCreature = new ChickenCreature(player.getPosition());
|
||||
chickenCreature.setInstance(player.getInstance());
|
||||
//chickenCreature.setTarget(player);
|
||||
}
|
||||
|
||||
/*EntityZombie zombie = new EntityZombie(player.getPosition());
|
||||
zombie.setAttribute(Attribute.MOVEMENT_SPEED, 0.25f);
|
||||
@ -175,13 +181,14 @@ public class PlayerInit {
|
||||
if (event.getHand() != Player.Hand.MAIN)
|
||||
return;
|
||||
|
||||
short blockId = player.getInstance().getBlockId(event.getBlockPosition());
|
||||
Block block = Block.fromId(blockId);
|
||||
final short blockStateId = player.getInstance().getBlockStateId(event.getBlockPosition());
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
player.sendMessage("You clicked at the block " + block);
|
||||
});
|
||||
|
||||
player.addEventCallback(PickupItemEvent.class, event -> {
|
||||
event.setCancelled(!player.getInventory().addItemStack(event.getItemStack())); // Cancel event if player does not have enough inventory space
|
||||
// Cancel event if player does not have enough inventory space
|
||||
event.setCancelled(!player.getInventory().addItemStack(event.getItemStack()));
|
||||
});
|
||||
|
||||
player.addEventCallback(ItemDropEvent.class, event -> {
|
||||
@ -248,7 +255,7 @@ public class PlayerInit {
|
||||
});
|
||||
|
||||
player.addEventCallback(PlayerSpawnEvent.class, event -> {
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.setGameMode(GameMode.SURVIVAL);
|
||||
player.teleport(new Position(0, 41f, 0));
|
||||
|
||||
//player.setHeldItemSlot((byte) 5);
|
||||
@ -294,7 +301,7 @@ public class PlayerInit {
|
||||
|
||||
Instance instance = player.getInstance();
|
||||
WorldBorder worldBorder = instance.getWorldBorder();
|
||||
worldBorder.setDiameter(30);
|
||||
//worldBorder.setDiameter(30);
|
||||
|
||||
//EntityBoat entityBoat = new EntityBoat(player.getPosition());
|
||||
//entityBoat.setInstance(player.getInstance());
|
||||
|
@ -1,14 +1,15 @@
|
||||
package fr.themode.demo.entity;
|
||||
|
||||
import net.minestom.server.attribute.Attribute;
|
||||
import net.minestom.server.entity.ai.goal.EatBlockGoal;
|
||||
import net.minestom.server.entity.ai.goal.RandomStrollGoal;
|
||||
import net.minestom.server.entity.ai.target.PlayerTarget;
|
||||
import net.minestom.server.entity.LivingEntity;
|
||||
import net.minestom.server.entity.ai.goal.MeleeAttackGoal;
|
||||
import net.minestom.server.entity.ai.target.ClosestEntityTarget;
|
||||
import net.minestom.server.entity.damage.DamageType;
|
||||
import net.minestom.server.entity.type.EntityChicken;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.event.entity.EntityAttackEvent;
|
||||
import net.minestom.server.utils.Position;
|
||||
|
||||
import java.util.HashMap;
|
||||
import net.minestom.server.utils.Vector;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
|
||||
public class ChickenCreature extends EntityChicken {
|
||||
|
||||
@ -16,8 +17,9 @@ public class ChickenCreature extends EntityChicken {
|
||||
super(defaultPosition);
|
||||
|
||||
//goalSelectors.add(new DoNothingGoal(this, 500, 0.1f));
|
||||
goalSelectors.add(new RandomStrollGoal(this, 2));
|
||||
goalSelectors.add(new EatBlockGoal(this,
|
||||
goalSelectors.add(new MeleeAttackGoal(this, 500, TimeUnit.MILLISECOND));
|
||||
//goalSelectors.add(new RandomStrollGoal(this, 2));
|
||||
/*goalSelectors.add(new EatBlockGoal(this,
|
||||
new HashMap<>() {
|
||||
{
|
||||
put(Block.GRASS.getBlockId(), Block.AIR.getBlockId());
|
||||
@ -30,14 +32,24 @@ public class ChickenCreature extends EntityChicken {
|
||||
},
|
||||
100))
|
||||
;
|
||||
//goalSelectors.add(new FollowTargetGoal(this));
|
||||
//goalSelectors.add(new FollowTargetGoal(this));*/
|
||||
|
||||
|
||||
targetSelectors.add(new PlayerTarget(this, 15));
|
||||
//targetSelectors.add(new LastEntityDamagerTarget(this, 15));
|
||||
targetSelectors.add(new ClosestEntityTarget(this, 15, LivingEntity.class));
|
||||
|
||||
|
||||
setAttribute(Attribute.MOVEMENT_SPEED, 0.1f);
|
||||
|
||||
addEventCallback(EntityAttackEvent.class, event -> {
|
||||
//System.out.println("CALL ATTACK");
|
||||
LivingEntity entity = (LivingEntity) event.getTarget();
|
||||
Vector velocity = getPosition().clone().getDirection().multiply(6);
|
||||
velocity.setY(4f);
|
||||
entity.damage(DamageType.fromEntity(this), -1);
|
||||
entity.setVelocity(velocity);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -138,8 +138,8 @@ public class CollisionUtils {
|
||||
blockPos.setX((int) Math.floor(corner.getX()));
|
||||
blockPos.setY((int) Math.floor(corner.getY()));
|
||||
blockPos.setZ((int) Math.floor(corner.getZ()));
|
||||
final short blockId = instance.getBlockId(blockPos);
|
||||
final Block block = Block.fromId(blockId);
|
||||
final short blockStateId = instance.getBlockStateId(blockPos);
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
|
||||
// TODO: block collision boxes
|
||||
// TODO: for the moment, always consider a full block
|
||||
|
@ -39,11 +39,12 @@ public class EatBlockGoal extends GoalSelector {
|
||||
if (RANDOM.nextInt(chancePerTick) != 0) {
|
||||
return false;
|
||||
}
|
||||
Instance instance = entityCreature.getInstance();
|
||||
final short blockIdIn = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1, 0));
|
||||
final short blockIdBelow = instance.getBlockId(entityCreature.getPosition().toBlockPosition().clone().subtract(0, 2, 0));
|
||||
final Instance instance = entityCreature.getInstance();
|
||||
final BlockPosition blockPosition = entityCreature.getPosition().toBlockPosition();
|
||||
final short blockStateIdIn = instance.getBlockStateId(blockPosition.clone().subtract(0, 1, 0));
|
||||
final short blockStateIdBelow = instance.getBlockStateId(blockPosition.clone().subtract(0, 2, 0));
|
||||
|
||||
return eatInMap.containsKey(blockIdIn) || eatBelowMap.containsKey(blockIdBelow);
|
||||
return eatInMap.containsKey(blockStateIdIn) || eatBelowMap.containsKey(blockStateIdBelow);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -64,12 +65,12 @@ public class EatBlockGoal extends GoalSelector {
|
||||
final BlockPosition currentPosition = entityCreature.getPosition().toBlockPosition().clone().subtract(0, 1, 0);
|
||||
final BlockPosition belowPosition = currentPosition.clone().subtract(0, 1, 0);
|
||||
|
||||
final short blockIdIn = instance.getBlockId(currentPosition);
|
||||
final short blockIdBelow = instance.getBlockId(belowPosition);
|
||||
if (eatInMap.containsKey(blockIdIn)) {
|
||||
instance.setBlock(currentPosition, eatInMap.get(blockIdIn));
|
||||
} else if (eatBelowMap.containsKey(blockIdBelow)) {
|
||||
instance.setBlock(belowPosition, eatBelowMap.get(blockIdBelow));
|
||||
final short blockStateIdIn = instance.getBlockStateId(currentPosition);
|
||||
final short blockStateIdBelow = instance.getBlockStateId(belowPosition);
|
||||
if (eatInMap.containsKey(blockStateIdIn)) {
|
||||
instance.setBlockStateId(currentPosition, eatInMap.get(blockStateIdIn));
|
||||
} else if (eatBelowMap.containsKey(blockStateIdBelow)) {
|
||||
instance.setBlockStateId(belowPosition, eatBelowMap.get(blockStateIdBelow));
|
||||
}
|
||||
// TODO: Call Entity Eat Animation
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ public class PFColumnarSpace implements IColumnarSpace {
|
||||
|
||||
@Override
|
||||
public IBlockDescription blockAt(int x, int y, int z) {
|
||||
final short blockId = chunk.getBlockId(x, y, z);
|
||||
final Block block = Block.fromId(blockId);
|
||||
final short blockStateId = chunk.getBlockStateId(x, y, z);
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
return new PFBlockDescription(block);
|
||||
}
|
||||
|
||||
|
@ -22,8 +22,8 @@ public class PFInstanceSpace implements IInstanceSpace {
|
||||
|
||||
@Override
|
||||
public IBlockObject blockObjectAt(int x, int y, int z) {
|
||||
final short blockId = instance.getBlockId(x, y, z);
|
||||
final Block block = Block.fromId(blockId);
|
||||
final short blockStateId = instance.getBlockStateId(x, y, z);
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
return new PFBlockObject(block);
|
||||
}
|
||||
|
||||
|
@ -11,23 +11,23 @@ public class PlayerBlockBreakEvent extends CancellableEvent {
|
||||
|
||||
private BlockPosition blockPosition;
|
||||
|
||||
private short blockId;
|
||||
private short blockStateId;
|
||||
private CustomBlock customBlock;
|
||||
|
||||
private short resultBlockId;
|
||||
private short resultBlockStateId;
|
||||
private short resultCustomBlockId;
|
||||
|
||||
public PlayerBlockBreakEvent(Player player, BlockPosition blockPosition,
|
||||
short blockId, CustomBlock customBlock,
|
||||
short resultBlockId, short resultCustomBlockId) {
|
||||
short blockStateId, CustomBlock customBlock,
|
||||
short resultBlockStateId, short resultCustomBlockId) {
|
||||
this.player = player;
|
||||
|
||||
this.blockPosition = blockPosition;
|
||||
|
||||
this.blockId = blockId;
|
||||
this.blockStateId = blockStateId;
|
||||
this.customBlock = customBlock;
|
||||
|
||||
this.resultBlockId = resultBlockId;
|
||||
this.resultBlockStateId = resultBlockStateId;
|
||||
this.resultCustomBlockId = resultCustomBlockId;
|
||||
}
|
||||
|
||||
@ -50,12 +50,12 @@ public class PlayerBlockBreakEvent extends CancellableEvent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the broken block visual id
|
||||
* Get the broken block state id
|
||||
*
|
||||
* @return the block id
|
||||
*/
|
||||
public short getBlockId() {
|
||||
return blockId;
|
||||
public short getBlockStateId() {
|
||||
return blockStateId;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,24 +74,24 @@ public class PlayerBlockBreakEvent extends CancellableEvent {
|
||||
* @return the block id that will be set at {@link #getBlockPosition()}
|
||||
* set to 0 to remove
|
||||
*/
|
||||
public short getResultBlockId() {
|
||||
return resultBlockId;
|
||||
public short getResultBlockStateId() {
|
||||
return resultBlockStateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the visual block id result
|
||||
*
|
||||
* @param resultBlockId the result block id
|
||||
* @param resultBlockStateId the result block id
|
||||
*/
|
||||
public void setResultBlockId(short resultBlockId) {
|
||||
this.resultBlockId = resultBlockId;
|
||||
public void setResultBlockId(short resultBlockStateId) {
|
||||
this.resultBlockStateId = resultBlockStateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the custom block id result, which will be placed after the event
|
||||
* <p>
|
||||
* Warning: the visual block will not be changed, be sure to call {@link #setResultBlockId(short)}
|
||||
* if you want the visual to be the same as {@link CustomBlock#getBlockId()}
|
||||
* if you want the visual to be the same as {@link CustomBlock#getBlockStateId()}
|
||||
*
|
||||
* @return the custom block id that will be set at {@link #getBlockPosition()}
|
||||
* set to 0 to remove
|
||||
|
@ -15,16 +15,16 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
|
||||
private static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
|
||||
|
||||
private final Player player;
|
||||
private short blockId;
|
||||
private short blockStateId;
|
||||
private short customBlockId;
|
||||
private BlockPosition blockPosition;
|
||||
private Player.Hand hand;
|
||||
|
||||
private boolean consumeBlock;
|
||||
|
||||
public PlayerBlockPlaceEvent(Player player, short blockId, short customBlockId, BlockPosition blockPosition, Player.Hand hand) {
|
||||
public PlayerBlockPlaceEvent(Player player, short blockStateId, short customBlockId, BlockPosition blockPosition, Player.Hand hand) {
|
||||
this.player = player;
|
||||
this.blockId = blockId;
|
||||
this.blockStateId = blockStateId;
|
||||
this.customBlockId = customBlockId;
|
||||
this.blockPosition = blockPosition;
|
||||
this.hand = hand;
|
||||
@ -37,12 +37,12 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
|
||||
* @param customBlock the custom block to place
|
||||
*/
|
||||
public void setCustomBlock(CustomBlock customBlock) {
|
||||
setBlockId(customBlock.getBlockId());
|
||||
setBlockStateId(customBlock.getBlockStateId());
|
||||
setCustomBlockId(customBlock.getCustomBlockId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set both the blockId and customBlockId
|
||||
* Set both the blockStateId and customBlockId
|
||||
*
|
||||
* @param customBlockId the custom block id to place
|
||||
*/
|
||||
@ -73,7 +73,7 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
|
||||
/**
|
||||
* Set the custom block id to place
|
||||
* <p>
|
||||
* WARNING: this does not change the visual block id, see {@link #setBlockId(short)}
|
||||
* WARNING: this does not change the visual block id, see {@link #setBlockStateId(short)}
|
||||
* or {@link #setCustomBlock(short)}
|
||||
*
|
||||
* @param customBlockId the custom block id
|
||||
@ -83,21 +83,21 @@ public class PlayerBlockPlaceEvent extends CancellableEvent {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the visual block id
|
||||
* Get the block state id
|
||||
*
|
||||
* @return the visual block id
|
||||
* @return the block state id
|
||||
*/
|
||||
public short getBlockId() {
|
||||
return blockId;
|
||||
public short getBlockStateId() {
|
||||
return blockStateId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the visual block id
|
||||
*
|
||||
* @param blockId the new visual block id
|
||||
* @param blockStateId the new block state id
|
||||
*/
|
||||
public void setBlockId(short blockId) {
|
||||
this.blockId = blockId;
|
||||
public void setBlockStateId(short blockStateId) {
|
||||
this.blockStateId = blockStateId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -6,50 +6,35 @@ import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.instance.block.BlockManager;
|
||||
import net.minestom.server.instance.block.CustomBlock;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
|
||||
public interface BlockModifier {
|
||||
|
||||
BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
|
||||
|
||||
void setBlock(int x, int y, int z, short blockId, Data data);
|
||||
void setBlockStateId(int x, int y, int z, short blockStateId, Data data);
|
||||
|
||||
void setCustomBlock(int x, int y, int z, short customBlockId, Data data);
|
||||
|
||||
void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data);
|
||||
|
||||
default void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId) {
|
||||
setSeparateBlocks(x, y, z, blockId, customBlockId, null);
|
||||
}
|
||||
|
||||
default void setBlock(int x, int y, int z, short blockId) {
|
||||
setBlock(x, y, z, blockId, null);
|
||||
default void setBlockStateId(int x, int y, int z, short blockStateId) {
|
||||
setBlockStateId(x, y, z, blockStateId, null);
|
||||
}
|
||||
|
||||
default void setBlock(int x, int y, int z, Block block) {
|
||||
setBlock(x, y, z, block.getBlockId(), null);
|
||||
}
|
||||
|
||||
default void setCustomBlock(int x, int y, int z, short customBlockId) {
|
||||
setCustomBlock(x, y, z, customBlockId, null);
|
||||
setBlockStateId(x, y, z, block.getBlockId(), null);
|
||||
}
|
||||
|
||||
default void setBlock(BlockPosition blockPosition, Block block) {
|
||||
setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block.getBlockId());
|
||||
setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), block);
|
||||
}
|
||||
|
||||
default void setBlock(BlockPosition blockPosition, short blockId) {
|
||||
setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockId);
|
||||
}
|
||||
|
||||
default void setBlock(Position position, Block block) {
|
||||
setBlock(position.toBlockPosition(), block.getBlockId());
|
||||
default void setBlockStateId(BlockPosition blockPosition, short blockStateId) {
|
||||
setBlockStateId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), blockStateId);
|
||||
}
|
||||
|
||||
|
||||
default void setBlock(Position position, short blockId) {
|
||||
setBlock(position.toBlockPosition(), blockId);
|
||||
void setCustomBlock(int x, int y, int z, short customBlockId, Data data);
|
||||
|
||||
default void setCustomBlock(int x, int y, int z, short customBlockId) {
|
||||
setCustomBlock(x, y, z, customBlockId, null);
|
||||
}
|
||||
|
||||
default void setCustomBlock(int x, int y, int z, String customBlockId, Data data) {
|
||||
@ -67,8 +52,10 @@ public interface BlockModifier {
|
||||
setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), customBlockId);
|
||||
}
|
||||
|
||||
default void setCustomBlock(Position position, String customBlockId) {
|
||||
setCustomBlock(position.toBlockPosition(), customBlockId);
|
||||
void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, Data data);
|
||||
|
||||
default void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId) {
|
||||
setSeparateBlocks(x, y, z, blockStateId, customBlockId, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public final class Chunk implements Viewable {
|
||||
private int chunkX, chunkZ;
|
||||
|
||||
// blocks id based on coord, see Chunk#getBlockIndex
|
||||
public short[] blocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
|
||||
public short[] blocksStateId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
|
||||
private short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
|
||||
|
||||
// Used to get all blocks with data (no null)
|
||||
@ -80,20 +80,20 @@ public final class Chunk implements Viewable {
|
||||
this.chunkZ = chunkZ;
|
||||
}
|
||||
|
||||
public void UNSAFE_setBlock(int x, int y, int z, short blockId, Data data) {
|
||||
setBlock(x, y, z, blockId, (short) 0, data, null);
|
||||
public void UNSAFE_setBlock(int x, int y, int z, short blockStateId, Data data) {
|
||||
setBlock(x, y, z, blockStateId, (short) 0, data, null);
|
||||
}
|
||||
|
||||
public void UNSAFE_setCustomBlock(int x, int y, int z, short visualBlockId, short customBlockId, Data data) {
|
||||
public void UNSAFE_setCustomBlock(int x, int y, int z, short blockStateId, short customBlockId, Data data) {
|
||||
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
||||
Check.notNull(customBlock, "The custom block " + customBlockId + " does not exist or isn't registered");
|
||||
|
||||
UNSAFE_setCustomBlock(x, y, z, visualBlockId, customBlock, data);
|
||||
UNSAFE_setCustomBlock(x, y, z, blockStateId, customBlock, data);
|
||||
}
|
||||
|
||||
protected void UNSAFE_setCustomBlock(int x, int y, int z, short visualBlockId, CustomBlock customBlock, Data data) {
|
||||
protected void UNSAFE_setCustomBlock(int x, int y, int z, short blockStateId, CustomBlock customBlock, Data data) {
|
||||
UpdateConsumer updateConsumer = customBlock.hasUpdate() ? customBlock::update : null;
|
||||
setBlock(x, y, z, visualBlockId, customBlock.getCustomBlockId(), data, updateConsumer);
|
||||
setBlock(x, y, z, blockStateId, customBlock.getCustomBlockId(), data, updateConsumer);
|
||||
}
|
||||
|
||||
public void UNSAFE_removeCustomBlock(int x, int y, int z) {
|
||||
@ -107,16 +107,16 @@ public final class Chunk implements Viewable {
|
||||
this.blockEntities.remove(index);
|
||||
}
|
||||
|
||||
private void setBlock(int x, int y, int z, short blockId, short customId, Data data, UpdateConsumer updateConsumer) {
|
||||
private void setBlock(int x, int y, int z, short blockStateId, short customId, Data data, UpdateConsumer updateConsumer) {
|
||||
final int index = getBlockIndex(x, y, z);
|
||||
if (blockId != 0
|
||||
|| (blockId == 0 && customId != 0 && updateConsumer != null)) { // Allow custom air block for update purpose, refused if no update consumer has been found
|
||||
this.blocksId[index] = blockId;
|
||||
if (blockStateId != 0
|
||||
|| (blockStateId == 0 && customId != 0 && updateConsumer != null)) { // Allow custom air block for update purpose, refused if no update consumer has been found
|
||||
this.blocksStateId[index] = blockStateId;
|
||||
this.customBlocksId[index] = customId;
|
||||
} else {
|
||||
// Block has been deleted, clear cache and return
|
||||
|
||||
this.blocksId[index] = 0; // Set to air
|
||||
this.blocksStateId[index] = 0; // Set to air
|
||||
|
||||
this.blocksData.remove(index);
|
||||
|
||||
@ -145,7 +145,7 @@ public final class Chunk implements Viewable {
|
||||
this.updatableBlocksLastUpdate.remove(index);
|
||||
}
|
||||
|
||||
if (isBlockEntity(blockId)) {
|
||||
if (isBlockEntity(blockStateId)) {
|
||||
this.blockEntities.add(index);
|
||||
} else {
|
||||
this.blockEntities.remove(index);
|
||||
@ -155,7 +155,7 @@ public final class Chunk implements Viewable {
|
||||
|
||||
if (columnarSpace != null) {
|
||||
final ColumnarOcclusionFieldList columnarOcclusionFieldList = columnarSpace.occlusionFields();
|
||||
final PFBlockDescription blockDescription = new PFBlockDescription(Block.fromId(blockId));
|
||||
final PFBlockDescription blockDescription = new PFBlockDescription(Block.fromStateId(blockStateId));
|
||||
columnarOcclusionFieldList.onBlockChanged(x, y, z, blockDescription, 0);
|
||||
}
|
||||
}
|
||||
@ -169,18 +169,18 @@ public final class Chunk implements Viewable {
|
||||
}
|
||||
}
|
||||
|
||||
public short getBlockId(int x, int y, int z) {
|
||||
public short getBlockStateId(int x, int y, int z) {
|
||||
final int index = getBlockIndex(x, y, z);
|
||||
if (!MathUtils.isBetween(index, 0, blocksId.length)) {
|
||||
if (!MathUtils.isBetween(index, 0, blocksStateId.length)) {
|
||||
return 0; // TODO: custom invalid block
|
||||
}
|
||||
final short id = blocksId[index];
|
||||
final short id = blocksStateId[index];
|
||||
return id;
|
||||
}
|
||||
|
||||
public short getCustomBlockId(int x, int y, int z) {
|
||||
final int index = getBlockIndex(x, y, z);
|
||||
if (!MathUtils.isBetween(index, 0, blocksId.length)) {
|
||||
if (!MathUtils.isBetween(index, 0, blocksStateId.length)) {
|
||||
return 0; // TODO: custom invalid block
|
||||
}
|
||||
final short id = customBlocksId[index];
|
||||
@ -189,7 +189,7 @@ public final class Chunk implements Viewable {
|
||||
|
||||
public CustomBlock getCustomBlock(int x, int y, int z) {
|
||||
final int index = getBlockIndex(x, y, z);
|
||||
if (!MathUtils.isBetween(index, 0, blocksId.length)) {
|
||||
if (!MathUtils.isBetween(index, 0, blocksStateId.length)) {
|
||||
return null; // TODO: custom invalid block
|
||||
}
|
||||
final short id = customBlocksId[index];
|
||||
@ -201,29 +201,29 @@ public final class Chunk implements Viewable {
|
||||
return getCustomBlock(pos[0], pos[1], pos[2]);
|
||||
}
|
||||
|
||||
protected void refreshBlockValue(int x, int y, int z, short blockId, short customId) {
|
||||
protected void refreshBlockValue(int x, int y, int z, short blockStateId, short customId) {
|
||||
final int blockIndex = getBlockIndex(x, y, z);
|
||||
if (!MathUtils.isBetween(blockIndex, 0, blocksId.length)) {
|
||||
if (!MathUtils.isBetween(blockIndex, 0, blocksStateId.length)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.blocksId[blockIndex] = blockId;
|
||||
this.blocksStateId[blockIndex] = blockStateId;
|
||||
this.customBlocksId[blockIndex] = customId;
|
||||
}
|
||||
|
||||
protected void refreshBlockId(int x, int y, int z, short blockId) {
|
||||
protected void refreshBlockStateId(int x, int y, int z, short blockStateId) {
|
||||
final int blockIndex = getBlockIndex(x, y, z);
|
||||
if (!MathUtils.isBetween(blockIndex, 0, blocksId.length)) {
|
||||
if (!MathUtils.isBetween(blockIndex, 0, blocksStateId.length)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.blocksId[blockIndex] = blockId;
|
||||
this.blocksStateId[blockIndex] = blockStateId;
|
||||
}
|
||||
|
||||
protected void refreshBlockValue(int x, int y, int z, short blockId) {
|
||||
protected void refreshBlockValue(int x, int y, int z, short blockStateId) {
|
||||
final CustomBlock customBlock = getCustomBlock(x, y, z);
|
||||
final short customBlockId = customBlock == null ? 0 : customBlock.getCustomBlockId();
|
||||
refreshBlockValue(x, y, z, blockId, customBlockId);
|
||||
refreshBlockValue(x, y, z, blockStateId, customBlockId);
|
||||
}
|
||||
|
||||
public Data getData(int x, int y, int z) {
|
||||
@ -281,8 +281,8 @@ public final class Chunk implements Viewable {
|
||||
return fullDataPacket;
|
||||
}
|
||||
|
||||
private boolean isBlockEntity(short blockId) {
|
||||
final Block block = Block.fromId(blockId);
|
||||
private boolean isBlockEntity(short blockStateId) {
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
return block.hasBlockEntity();
|
||||
}
|
||||
|
||||
@ -328,10 +328,10 @@ public final class Chunk implements Viewable {
|
||||
for (byte z = 0; z < CHUNK_SIZE_Z; z++) {
|
||||
final int index = getBlockIndex(x, y, z);
|
||||
|
||||
final short blockId = blocksId[index];
|
||||
final short blockStateId = blocksStateId[index];
|
||||
final short customBlockId = customBlocksId[index];
|
||||
|
||||
if (blockId == 0 && customBlockId == 0)
|
||||
if (blockStateId == 0 && customBlockId == 0)
|
||||
continue;
|
||||
|
||||
final Data data = blocksData.get(index);
|
||||
@ -342,7 +342,7 @@ public final class Chunk implements Viewable {
|
||||
dos.writeInt(z);
|
||||
|
||||
// Id
|
||||
dos.writeShort(blockId);
|
||||
dos.writeShort(blockStateId);
|
||||
dos.writeShort(customBlockId);
|
||||
|
||||
// Data
|
||||
@ -381,7 +381,7 @@ public final class Chunk implements Viewable {
|
||||
fullDataPacket.biomes = biomes.clone();
|
||||
fullDataPacket.chunkX = chunkX;
|
||||
fullDataPacket.chunkZ = chunkZ;
|
||||
fullDataPacket.blocksId = blocksId.clone();
|
||||
fullDataPacket.blocksStateId = blocksStateId.clone();
|
||||
fullDataPacket.customBlocksId = customBlocksId.clone();
|
||||
fullDataPacket.blockEntities = new CopyOnWriteArraySet<>(blockEntities);
|
||||
fullDataPacket.blocksData = new Int2ObjectOpenHashMap<>(blocksData);
|
||||
|
@ -100,9 +100,9 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
* In case of a CustomBlock it does not remove it but only refresh its visual
|
||||
*
|
||||
* @param blockPosition the block position
|
||||
* @param blockId the new block id
|
||||
* @param blockStateId the new block state
|
||||
*/
|
||||
public abstract void refreshBlockId(BlockPosition blockPosition, short blockId);
|
||||
public abstract void refreshBlockStateId(BlockPosition blockPosition, short blockStateId);
|
||||
|
||||
/**
|
||||
* Does call {@link net.minestom.server.event.player.PlayerBlockBreakEvent}
|
||||
@ -467,13 +467,13 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
* <p>
|
||||
* WARNING: the custom block id at the position will not change
|
||||
*
|
||||
* @param x the X position
|
||||
* @param y the Y position
|
||||
* @param z the Z position
|
||||
* @param blockId the new visual block id
|
||||
* @param x the X position
|
||||
* @param y the Y position
|
||||
* @param z the Z position
|
||||
* @param blockStateId the new block state id
|
||||
*/
|
||||
public void refreshBlockId(int x, int y, int z, short blockId) {
|
||||
refreshBlockId(new BlockPosition(x, y, z), blockId);
|
||||
public void refreshBlockStateId(int x, int y, int z, short blockStateId) {
|
||||
refreshBlockStateId(new BlockPosition(x, y, z), blockStateId);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -487,7 +487,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
* @param block the new visual block
|
||||
*/
|
||||
public void refreshBlockId(int x, int y, int z, Block block) {
|
||||
refreshBlockId(x, y, z, block.getBlockId());
|
||||
refreshBlockStateId(x, y, z, block.getBlockId());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -499,7 +499,7 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
* @param block the new visual block
|
||||
*/
|
||||
public void refreshBlockId(BlockPosition blockPosition, Block block) {
|
||||
refreshBlockId(blockPosition, block.getBlockId());
|
||||
refreshBlockStateId(blockPosition, block.getBlockId());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -543,39 +543,39 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the visual block id at the given position
|
||||
* Give the block state id at the given position
|
||||
*
|
||||
* @param x the X position
|
||||
* @param y the Y position
|
||||
* @param z the Z position
|
||||
* @return the visual block id at the position
|
||||
*/
|
||||
public short getBlockId(int x, int y, int z) {
|
||||
public short getBlockStateId(int x, int y, int z) {
|
||||
final Chunk chunk = getChunkAt(x, z);
|
||||
Check.notNull(chunk, "The chunk at " + x + ":" + z + " is not loaded");
|
||||
return chunk.getBlockId(x, y, z);
|
||||
return chunk.getBlockStateId(x, y, z);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the visual block id at the given position
|
||||
* Give the block state id at the given position
|
||||
*
|
||||
* @param x the X position
|
||||
* @param y the Y position
|
||||
* @param z the Z position
|
||||
* @return the visual block id at the position
|
||||
*/
|
||||
public short getBlockId(float x, float y, float z) {
|
||||
return getBlockId(Math.round(x), Math.round(y), Math.round(z));
|
||||
public short getBlockStateId(float x, float y, float z) {
|
||||
return getBlockStateId(Math.round(x), Math.round(y), Math.round(z));
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the visual block id at the given position
|
||||
* Give the block state id at the given position
|
||||
*
|
||||
* @param blockPosition the block position
|
||||
* @return the visual block id at the position
|
||||
*/
|
||||
public short getBlockId(BlockPosition blockPosition) {
|
||||
return getBlockId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
|
||||
public short getBlockStateId(BlockPosition blockPosition) {
|
||||
return getBlockStateId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -603,8 +603,8 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
}
|
||||
|
||||
public void sendBlockAction(BlockPosition blockPosition, byte actionId, byte actionParam) {
|
||||
final short blockId = getBlockId(blockPosition);
|
||||
final Block block = Block.fromId(blockId);
|
||||
final short blockStateId = getBlockStateId(blockPosition);
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
|
||||
BlockActionPacket blockActionPacket = new BlockActionPacket();
|
||||
blockActionPacket.blockPosition = blockPosition;
|
||||
|
@ -78,23 +78,23 @@ public class InstanceContainer extends Instance {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, short blockId, Data data) {
|
||||
setBlock(x, y, z, blockId, null, data);
|
||||
public void setBlockStateId(int x, int y, int z, short blockStateId, Data data) {
|
||||
setBlock(x, y, z, blockStateId, null, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCustomBlock(int x, int y, int z, short customBlockId, Data data) {
|
||||
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
||||
setBlock(x, y, z, customBlock.getBlockId(), customBlock, data);
|
||||
setBlock(x, y, z, customBlock.getBlockStateId(), customBlock, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
|
||||
public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, Data data) {
|
||||
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
||||
setBlock(x, y, z, blockId, customBlock, data);
|
||||
setBlock(x, y, z, blockStateId, customBlock, data);
|
||||
}
|
||||
|
||||
private synchronized void setBlock(int x, int y, int z, short blockId, CustomBlock customBlock, Data data) {
|
||||
private synchronized void setBlock(int x, int y, int z, short blockStateId, CustomBlock customBlock, Data data) {
|
||||
final Chunk chunk = getChunkAt(x, z);
|
||||
synchronized (chunk) {
|
||||
|
||||
@ -102,32 +102,32 @@ public class InstanceContainer extends Instance {
|
||||
|
||||
final BlockPosition blockPosition = new BlockPosition(x, y, z);
|
||||
|
||||
if (isAlreadyChanged(blockPosition, blockId)) { // do NOT change the block again.
|
||||
if (isAlreadyChanged(blockPosition, blockStateId)) { // do NOT change the block again.
|
||||
// Avoids StackOverflowExceptions when onDestroy tries to destroy the block itself
|
||||
// This can happen with nether portals which break the entire frame when a portal block is broken
|
||||
return;
|
||||
}
|
||||
setAlreadyChanged(blockPosition, blockId);
|
||||
setAlreadyChanged(blockPosition, blockStateId);
|
||||
final int index = ChunkUtils.getBlockIndex(x, y, z);
|
||||
|
||||
// Call the destroy listener if previous block was a custom block
|
||||
callBlockDestroy(chunk, index, blockPosition);
|
||||
// Change id based on neighbors
|
||||
blockId = executeBlockPlacementRule(blockId, blockPosition);
|
||||
blockStateId = executeBlockPlacementRule(blockStateId, blockPosition);
|
||||
|
||||
// Set the block
|
||||
if (isCustomBlock) {
|
||||
data = customBlock.createData(this, blockPosition, data);
|
||||
chunk.UNSAFE_setCustomBlock(x, y, z, blockId, customBlock, data);
|
||||
chunk.UNSAFE_setCustomBlock(x, y, z, blockStateId, customBlock, data);
|
||||
} else {
|
||||
chunk.UNSAFE_setBlock(x, y, z, blockId, data);
|
||||
chunk.UNSAFE_setBlock(x, y, z, blockStateId, data);
|
||||
}
|
||||
|
||||
// Refresh neighbors since a new block has been placed
|
||||
executeNeighboursBlockPlacementRule(blockPosition);
|
||||
|
||||
// Refresh player chunk block
|
||||
sendBlockChange(chunk, blockPosition, blockId);
|
||||
sendBlockChange(chunk, blockPosition, blockStateId);
|
||||
|
||||
// Call the place listener for custom block
|
||||
if (isCustomBlock)
|
||||
@ -135,32 +135,32 @@ public class InstanceContainer extends Instance {
|
||||
}
|
||||
}
|
||||
|
||||
private void setAlreadyChanged(BlockPosition blockPosition, short blockId) {
|
||||
currentlyChangingBlocks.put(blockPosition, Block.fromId(blockId));
|
||||
private void setAlreadyChanged(BlockPosition blockPosition, short blockStateId) {
|
||||
currentlyChangingBlocks.put(blockPosition, Block.fromStateId(blockStateId));
|
||||
}
|
||||
|
||||
/**
|
||||
* Has this block already changed since last update? Prevents StackOverflow with blocks trying to modify their position in onDestroy or onPlace
|
||||
*
|
||||
* @param blockPosition the block position
|
||||
* @param blockId the block id
|
||||
* @param blockStateId the block state id
|
||||
* @return
|
||||
*/
|
||||
private boolean isAlreadyChanged(BlockPosition blockPosition, short blockId) {
|
||||
private boolean isAlreadyChanged(BlockPosition blockPosition, short blockStateId) {
|
||||
final Block changedBlock = currentlyChangingBlocks.get(blockPosition);
|
||||
if (changedBlock == null)
|
||||
return false;
|
||||
return changedBlock.getBlockId() == blockId;
|
||||
return changedBlock.getBlockId() == blockStateId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshBlockId(BlockPosition blockPosition, short blockId) {
|
||||
public void refreshBlockStateId(BlockPosition blockPosition, short blockStateId) {
|
||||
final Chunk chunk = getChunkAt(blockPosition.getX(), blockPosition.getZ());
|
||||
synchronized (chunk) {
|
||||
chunk.refreshBlockId(blockPosition.getX(), blockPosition.getY(),
|
||||
blockPosition.getZ(), blockId);
|
||||
chunk.refreshBlockStateId(blockPosition.getX(), blockPosition.getY(),
|
||||
blockPosition.getZ(), blockStateId);
|
||||
|
||||
sendBlockChange(chunk, blockPosition, blockId);
|
||||
sendBlockChange(chunk, blockPosition, blockStateId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -179,12 +179,12 @@ public class InstanceContainer extends Instance {
|
||||
actualBlock.onPlace(this, blockPosition, previousData);
|
||||
}
|
||||
|
||||
private short executeBlockPlacementRule(short blockId, BlockPosition blockPosition) {
|
||||
final BlockPlacementRule blockPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(blockId);
|
||||
private short executeBlockPlacementRule(short blockStateId, BlockPosition blockPosition) {
|
||||
final BlockPlacementRule blockPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(blockStateId);
|
||||
if (blockPlacementRule != null) {
|
||||
return blockPlacementRule.blockRefresh(this, blockPosition, blockId);
|
||||
return blockPlacementRule.blockRefresh(this, blockPosition, blockStateId);
|
||||
}
|
||||
return blockId;
|
||||
return blockStateId;
|
||||
}
|
||||
|
||||
private void executeNeighboursBlockPlacementRule(BlockPosition blockPosition) {
|
||||
@ -202,13 +202,13 @@ public class InstanceContainer extends Instance {
|
||||
if (chunk == null)
|
||||
continue;
|
||||
|
||||
final short neighborId = chunk.getBlockId(neighborX, neighborY, neighborZ);
|
||||
final BlockPlacementRule neighborBlockPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(neighborId);
|
||||
final short neighborStateId = chunk.getBlockStateId(neighborX, neighborY, neighborZ);
|
||||
final BlockPlacementRule neighborBlockPlacementRule = BLOCK_MANAGER.getBlockPlacementRule(neighborStateId);
|
||||
if (neighborBlockPlacementRule != null) {
|
||||
final short newNeighborId = neighborBlockPlacementRule.blockRefresh(this,
|
||||
new BlockPosition(neighborX, neighborY, neighborZ), neighborId);
|
||||
if (neighborId != newNeighborId) {
|
||||
refreshBlockId(neighborX, neighborY, neighborZ, newNeighborId);
|
||||
new BlockPosition(neighborX, neighborY, neighborZ), neighborStateId);
|
||||
if (neighborStateId != newNeighborId) {
|
||||
refreshBlockStateId(neighborX, neighborY, neighborZ, neighborStateId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -242,28 +242,28 @@ public class InstanceContainer extends Instance {
|
||||
final int y = blockPosition.getY();
|
||||
final int z = blockPosition.getZ();
|
||||
|
||||
final short blockId = getBlockId(x, y, z);
|
||||
final short blockStateId = getBlockStateId(x, y, z);
|
||||
|
||||
// The player probably have a wrong version of this chunk section, send it
|
||||
if (blockId == 0) {
|
||||
if (blockStateId == 0) {
|
||||
sendChunkSectionUpdate(chunk, ChunkUtils.getSectionAt(y), player);
|
||||
return false;
|
||||
}
|
||||
|
||||
final CustomBlock customBlock = getCustomBlock(x, y, z);
|
||||
|
||||
PlayerBlockBreakEvent blockBreakEvent = new PlayerBlockBreakEvent(player, blockPosition, blockId, customBlock, (short) 0, (short) 0);
|
||||
PlayerBlockBreakEvent blockBreakEvent = new PlayerBlockBreakEvent(player, blockPosition, blockStateId, customBlock, (short) 0, (short) 0);
|
||||
player.callEvent(PlayerBlockBreakEvent.class, blockBreakEvent);
|
||||
final boolean result = !blockBreakEvent.isCancelled();
|
||||
if (result) {
|
||||
// Break or change the broken block based on event result
|
||||
setSeparateBlocks(x, y, z, blockBreakEvent.getResultBlockId(), blockBreakEvent.getResultCustomBlockId());
|
||||
setSeparateBlocks(x, y, z, blockBreakEvent.getResultBlockStateId(), blockBreakEvent.getResultCustomBlockId());
|
||||
|
||||
ParticlePacket particlePacket = ParticleCreator.createParticlePacket(Particle.BLOCK, false,
|
||||
x + 0.5f, y, z + 0.5f,
|
||||
0.4f, 0.5f, 0.4f,
|
||||
0.3f, 125, writer -> {
|
||||
writer.writeVarInt(blockId);
|
||||
writer.writeVarInt(blockStateId);
|
||||
});
|
||||
|
||||
chunk.getViewers().forEach(p -> {
|
||||
@ -579,10 +579,10 @@ public class InstanceContainer extends Instance {
|
||||
this.chunkLoader = chunkLoader;
|
||||
}
|
||||
|
||||
private void sendBlockChange(Chunk chunk, BlockPosition blockPosition, short blockId) {
|
||||
private void sendBlockChange(Chunk chunk, BlockPosition blockPosition, short blockStateId) {
|
||||
BlockChangePacket blockChangePacket = new BlockChangePacket();
|
||||
blockChangePacket.blockPosition = blockPosition;
|
||||
blockChangePacket.blockId = blockId;
|
||||
blockChangePacket.blockStateId = blockStateId;
|
||||
chunk.sendPacketToViewers(blockChangePacket);
|
||||
}
|
||||
|
||||
|
@ -27,8 +27,8 @@ public class SharedInstance extends Instance {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refreshBlockId(BlockPosition blockPosition, short blockId) {
|
||||
instanceContainer.refreshBlockId(blockPosition, blockId);
|
||||
public void refreshBlockStateId(BlockPosition blockPosition, short blockStateId) {
|
||||
instanceContainer.refreshBlockStateId(blockPosition, blockStateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -142,18 +142,18 @@ public class SharedInstance extends Instance {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, short blockId, Data data) {
|
||||
instanceContainer.setBlock(x, y, z, blockId, data);
|
||||
public void setBlockStateId(int x, int y, int z, short blockStateId, Data data) {
|
||||
instanceContainer.setBlockStateId(x, y, z, blockStateId, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCustomBlock(int x, int y, int z, short customBlockId, Data data) {
|
||||
instanceContainer.setBlock(x, y, z, customBlockId, data);
|
||||
instanceContainer.setCustomBlock(x, y, z, customBlockId, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
|
||||
instanceContainer.setSeparateBlocks(x, y, z, blockId, customBlockId, data);
|
||||
public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, Data data) {
|
||||
instanceContainer.setSeparateBlocks(x, y, z, blockStateId, customBlockId, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,25 +21,25 @@ public class BlockBatch implements InstanceBatch {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void setBlock(int x, int y, int z, short blockId, Data data) {
|
||||
Chunk chunk = this.instance.getChunkAt(x, z);
|
||||
addBlockData(chunk, x, y, z, false, blockId, (short) 0, data);
|
||||
public synchronized void setBlockStateId(int x, int y, int z, short blockStateId, Data data) {
|
||||
final Chunk chunk = this.instance.getChunkAt(x, z);
|
||||
addBlockData(chunk, x, y, z, false, blockStateId, (short) 0, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void setCustomBlock(int x, int y, int z, short blockId, Data data) {
|
||||
Chunk chunk = this.instance.getChunkAt(x, z);
|
||||
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(blockId);
|
||||
addBlockData(chunk, x, y, z, true, customBlock.getBlockId(), blockId, data);
|
||||
public void setCustomBlock(int x, int y, int z, short customBlockId, Data data) {
|
||||
final Chunk chunk = this.instance.getChunkAt(x, z);
|
||||
final CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
||||
addBlockData(chunk, x, y, z, true, customBlock.getBlockStateId(), customBlockId, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
|
||||
Chunk chunk = this.instance.getChunkAt(x, z);
|
||||
addBlockData(chunk, x, y, z, true, blockId, customBlockId, data);
|
||||
public synchronized void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, Data data) {
|
||||
final Chunk chunk = this.instance.getChunkAt(x, z);
|
||||
addBlockData(chunk, x, y, z, true, blockStateId, customBlockId, data);
|
||||
}
|
||||
|
||||
private void addBlockData(Chunk chunk, int x, int y, int z, boolean customBlock, short blockId, short customBlockId, Data data) {
|
||||
private void addBlockData(Chunk chunk, int x, int y, int z, boolean customBlock, short blockStateId, short customBlockId, Data data) {
|
||||
List<BlockData> blocksData = this.data.get(chunk);
|
||||
if (blocksData == null)
|
||||
blocksData = new ArrayList<>();
|
||||
@ -49,7 +49,7 @@ public class BlockBatch implements InstanceBatch {
|
||||
blockData.y = y;
|
||||
blockData.z = z;
|
||||
blockData.hasCustomBlock = customBlock;
|
||||
blockData.blockId = blockId;
|
||||
blockData.blockStateId = blockStateId;
|
||||
blockData.customBlockId = customBlockId;
|
||||
blockData.data = data;
|
||||
|
||||
@ -92,15 +92,15 @@ public class BlockBatch implements InstanceBatch {
|
||||
|
||||
private int x, y, z;
|
||||
private boolean hasCustomBlock;
|
||||
private short blockId;
|
||||
private short blockStateId;
|
||||
private short customBlockId;
|
||||
private Data data;
|
||||
|
||||
public void apply(Chunk chunk) {
|
||||
if (!hasCustomBlock) {
|
||||
chunk.UNSAFE_setBlock(x, y, z, blockId, data);
|
||||
chunk.UNSAFE_setBlock(x, y, z, blockStateId, data);
|
||||
} else {
|
||||
chunk.UNSAFE_setCustomBlock(x, y, z, blockId, customBlockId, data);
|
||||
chunk.UNSAFE_setCustomBlock(x, y, z, blockStateId, customBlockId, data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,29 +32,29 @@ public class ChunkBatch implements InstanceBatch {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlock(int x, int y, int z, short blockId, Data data) {
|
||||
addBlockData((byte) x, y, (byte) z, false, blockId, (short) 0, data);
|
||||
public void setBlockStateId(int x, int y, int z, short blockStateId, Data data) {
|
||||
addBlockData((byte) x, y, (byte) z, false, blockStateId, (short) 0, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCustomBlock(int x, int y, int z, short blockId, Data data) {
|
||||
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(blockId);
|
||||
addBlockData((byte) x, y, (byte) z, true, customBlock.getBlockId(), blockId, data);
|
||||
public void setCustomBlock(int x, int y, int z, short customBlockId, Data data) {
|
||||
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
||||
addBlockData((byte) x, y, (byte) z, true, customBlock.getBlockStateId(), customBlockId, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
|
||||
addBlockData((byte) x, y, (byte) z, true, blockId, customBlockId, data);
|
||||
public void setSeparateBlocks(int x, int y, int z, short blockStateId, short customBlockId, Data data) {
|
||||
addBlockData((byte) x, y, (byte) z, true, blockStateId, customBlockId, data);
|
||||
}
|
||||
|
||||
private void addBlockData(byte x, int y, byte z, boolean customBlock, short blockId, short customBlockId, Data data) {
|
||||
private void addBlockData(byte x, int y, byte z, boolean customBlock, short blockStateId, short customBlockId, Data data) {
|
||||
// TODO store a single long with bitwise operators (xyz;boolean,short,short,boolean) with the data in a map
|
||||
BlockData blockData = new BlockData();
|
||||
blockData.x = x;
|
||||
blockData.y = y;
|
||||
blockData.z = z;
|
||||
blockData.hasCustomBlock = customBlock;
|
||||
blockData.blockId = blockId;
|
||||
blockData.blockStateId = blockStateId;
|
||||
blockData.customBlockId = customBlockId;
|
||||
blockData.data = data;
|
||||
|
||||
@ -115,15 +115,15 @@ public class ChunkBatch implements InstanceBatch {
|
||||
|
||||
private int x, y, z;
|
||||
private boolean hasCustomBlock;
|
||||
private short blockId;
|
||||
private short blockStateId;
|
||||
private short customBlockId;
|
||||
private Data data;
|
||||
|
||||
public void apply(Chunk chunk) {
|
||||
if (!hasCustomBlock) {
|
||||
chunk.UNSAFE_setBlock(x, y, z, blockId, data);
|
||||
chunk.UNSAFE_setBlock(x, y, z, blockStateId, data);
|
||||
} else {
|
||||
chunk.UNSAFE_setCustomBlock(x, y, z, blockId, customBlockId, data);
|
||||
chunk.UNSAFE_setCustomBlock(x, y, z, blockStateId, customBlockId, data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,12 +41,12 @@ public class BlockManager {
|
||||
/**
|
||||
* Get the block placement rule of the specific block
|
||||
*
|
||||
* @param blockId the block id to check
|
||||
* @param blockStateId the block id to check
|
||||
* @return the block placement rule associated with the id, null if not any
|
||||
*/
|
||||
public BlockPlacementRule getBlockPlacementRule(short blockId) {
|
||||
final Block block = Block.fromId(blockId); // Convert block alternative
|
||||
blockId = block.getBlockId();
|
||||
public BlockPlacementRule getBlockPlacementRule(short blockStateId) {
|
||||
final Block block = Block.fromStateId(blockStateId); // Convert block alternative
|
||||
final short blockId = block.getBlockId();
|
||||
return this.placementRules.get(blockId);
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@ import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.gamedata.loottables.LootTable;
|
||||
import net.minestom.server.gamedata.loottables.LootTableManager;
|
||||
import net.minestom.server.instance.BlockModifier;
|
||||
import net.minestom.server.instance.Instance;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.time.UpdateOption;
|
||||
@ -24,15 +25,15 @@ public abstract class CustomBlock {
|
||||
* - option to set the global as "global breaking" meaning that multiple players mining the same block will break it faster (accumulation)
|
||||
*/
|
||||
|
||||
private final short blockId;
|
||||
private final short blockStateId;
|
||||
private final String identifier;
|
||||
|
||||
/**
|
||||
* @param blockId the visual block id
|
||||
* @param identifier the custom block identifier
|
||||
* @param blockStateId the block state id
|
||||
* @param identifier the custom block identifier
|
||||
*/
|
||||
public CustomBlock(short blockId, String identifier) {
|
||||
this.blockId = blockId;
|
||||
public CustomBlock(short blockStateId, String identifier) {
|
||||
this.blockStateId = blockStateId;
|
||||
this.identifier = identifier;
|
||||
}
|
||||
|
||||
@ -137,16 +138,16 @@ public abstract class CustomBlock {
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the default visual for the block when the custom block is set,
|
||||
* This is the default block state id when the custom block is set,
|
||||
* it is possible to change this value per block using
|
||||
* {@link net.minestom.server.instance.BlockModifier#setSeparateBlocks(int, int, int, short, short)}
|
||||
* {@link BlockModifier#setSeparateBlocks(int, int, int, short, short)}
|
||||
* <p>
|
||||
* Meaning that you should not believe that your custom blocks id will always be this one.
|
||||
*
|
||||
* @return the default visual block id
|
||||
*/
|
||||
public short getBlockId() {
|
||||
return blockId;
|
||||
public short getBlockStateId() {
|
||||
return blockStateId;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -81,13 +81,13 @@ public class RedstonePlacementRule extends BlockPlacementRule {
|
||||
}
|
||||
|
||||
private boolean isRedstone(Instance instance, int x, int y, int z) {
|
||||
short blockId = instance.getBlockId(x, y, z);
|
||||
return Block.fromId(blockId) == Block.REDSTONE_WIRE;
|
||||
final short blockStateId = instance.getBlockStateId(x, y, z);
|
||||
return Block.fromStateId(blockStateId) == Block.REDSTONE_WIRE;
|
||||
}
|
||||
|
||||
private boolean isAir(Instance instance, int x, int y, int z) {
|
||||
short blockId = instance.getBlockId(x, y, z);
|
||||
return Block.fromId(blockId) == Block.AIR;
|
||||
final short blockStateId = instance.getBlockStateId(x, y, z);
|
||||
return Block.fromStateId(blockStateId) == Block.AIR;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -61,8 +61,8 @@ public class WallPlacementRule extends BlockPlacementRule {
|
||||
}
|
||||
|
||||
private boolean isBlock(Instance instance, int x, int y, int z) {
|
||||
short blockId = instance.getBlockId(x, y, z);
|
||||
return Block.fromId(blockId).isSolid();
|
||||
final short blockStateId = instance.getBlockStateId(x, y, z);
|
||||
return Block.fromStateId(blockStateId).isSolid();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -90,10 +90,10 @@ public class BlockPlacementListener {
|
||||
// BlockPlacementRule check
|
||||
final BlockManager blockManager = MinecraftServer.getBlockManager();
|
||||
final BlockPlacementRule blockPlacementRule = blockManager.getBlockPlacementRule(block);
|
||||
final short blockId = blockPlacementRule == null ? block.getBlockId() :
|
||||
final short blockStateId = blockPlacementRule == null ? block.getBlockId() :
|
||||
blockPlacementRule.blockPlace(instance, block, blockFace, player);
|
||||
|
||||
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(player, blockId, (short) 0, blockPosition, packet.hand);
|
||||
PlayerBlockPlaceEvent playerBlockPlaceEvent = new PlayerBlockPlaceEvent(player, blockStateId, (short) 0, blockPosition, packet.hand);
|
||||
playerBlockPlaceEvent.consumeBlock(player.getGameMode() != GameMode.CREATIVE);
|
||||
|
||||
// BlockPlacementRule check
|
||||
@ -103,9 +103,9 @@ public class BlockPlacementListener {
|
||||
if (!playerBlockPlaceEvent.isCancelled() && canPlace) {
|
||||
final short customBlockId = playerBlockPlaceEvent.getCustomBlockId();
|
||||
if (customBlockId != 0) {
|
||||
instance.setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockId(), playerBlockPlaceEvent.getCustomBlockId());
|
||||
instance.setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockStateId(), playerBlockPlaceEvent.getCustomBlockId());
|
||||
} else {
|
||||
instance.setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockId());
|
||||
instance.setBlockStateId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockStateId());
|
||||
}
|
||||
if (playerBlockPlaceEvent.doesConsumeBlock()) {
|
||||
// Consume the block in the player's hand
|
||||
|
@ -30,13 +30,13 @@ public class PlayerDiggingListener {
|
||||
if (instance == null)
|
||||
return;
|
||||
|
||||
final short blockId = instance.getBlockId(blockPosition);
|
||||
final short blockStateId = instance.getBlockStateId(blockPosition);
|
||||
|
||||
switch (status) {
|
||||
case STARTED_DIGGING:
|
||||
final boolean instantBreak = player.isCreative() ||
|
||||
player.isInstantBreak() ||
|
||||
Block.fromId(blockId).breaksInstantaneously();
|
||||
Block.fromStateId(blockStateId).breaksInstantaneously();
|
||||
|
||||
if (instantBreak) {
|
||||
breakBlock(instance, player, blockPosition);
|
||||
@ -54,11 +54,11 @@ public class PlayerDiggingListener {
|
||||
player.setTargetBlock(customBlock, blockPosition, breakTime);
|
||||
addEffect(player);
|
||||
}
|
||||
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
|
||||
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockStateId(),
|
||||
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, true);
|
||||
} else {
|
||||
// Unsuccessful digging
|
||||
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
|
||||
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockStateId(),
|
||||
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false);
|
||||
}
|
||||
} else {
|
||||
@ -71,7 +71,7 @@ public class PlayerDiggingListener {
|
||||
// Remove custom block target
|
||||
removeEffect(player);
|
||||
|
||||
sendAcknowledgePacket(player, blockPosition, blockId,
|
||||
sendAcknowledgePacket(player, blockPosition, blockStateId,
|
||||
ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING, true);
|
||||
break;
|
||||
case FINISHED_DIGGING:
|
||||
@ -151,11 +151,11 @@ public class PlayerDiggingListener {
|
||||
player.resetTargetBlock();
|
||||
}
|
||||
|
||||
private static void sendAcknowledgePacket(Player player, BlockPosition blockPosition, int blockId,
|
||||
private static void sendAcknowledgePacket(Player player, BlockPosition blockPosition, int blockStateId,
|
||||
ClientPlayerDiggingPacket.Status status, boolean success) {
|
||||
AcknowledgePlayerDiggingPacket acknowledgePlayerDiggingPacket = new AcknowledgePlayerDiggingPacket();
|
||||
acknowledgePlayerDiggingPacket.blockPosition = blockPosition;
|
||||
acknowledgePlayerDiggingPacket.blockStateId = blockId;
|
||||
acknowledgePlayerDiggingPacket.blockStateId = blockStateId;
|
||||
acknowledgePlayerDiggingPacket.status = status;
|
||||
acknowledgePlayerDiggingPacket.successful = success;
|
||||
|
||||
|
@ -8,12 +8,12 @@ import net.minestom.server.utils.BlockPosition;
|
||||
public class BlockChangePacket implements ServerPacket {
|
||||
|
||||
public BlockPosition blockPosition;
|
||||
public int blockId;
|
||||
public int blockStateId;
|
||||
|
||||
@Override
|
||||
public void write(PacketWriter writer) {
|
||||
writer.writeBlockPosition(blockPosition);
|
||||
writer.writeVarInt(blockId);
|
||||
writer.writeVarInt(blockStateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -29,7 +29,7 @@ public class ChunkDataPacket implements ServerPacket {
|
||||
public Biome[] biomes;
|
||||
public int chunkX, chunkZ;
|
||||
|
||||
public short[] blocksId;
|
||||
public short[] blocksStateId;
|
||||
public short[] customBlocksId;
|
||||
|
||||
public Set<Integer> blockEntities;
|
||||
@ -124,14 +124,14 @@ public class ChunkDataPacket implements ServerPacket {
|
||||
for (byte y = 0; y < Chunk.CHUNK_SECTION_SIZE; y++) {
|
||||
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++) {
|
||||
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
|
||||
int yPos = (y + Chunk.CHUNK_SECTION_SIZE * section);
|
||||
int index = ChunkUtils.getBlockIndex(x, yPos, z);
|
||||
short blockId = blocksId[index];
|
||||
if (blockId != 0)
|
||||
final int yPos = (y + Chunk.CHUNK_SECTION_SIZE * section);
|
||||
final int index = ChunkUtils.getBlockIndex(x, yPos, z);
|
||||
final short blockStateId = blocksStateId[index];
|
||||
if (blockStateId != 0)
|
||||
empty = false;
|
||||
|
||||
int packetIndex = (((y * 16) + x) * 16) + z;
|
||||
blocks[packetIndex] = blockId;
|
||||
final int packetIndex = (((y * 16) + x) * 16) + z;
|
||||
blocks[packetIndex] = blockStateId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public class ChunkReader {
|
||||
final int y = stream.readInt();
|
||||
final int z = stream.readInt();
|
||||
|
||||
final short blockId = stream.readShort();
|
||||
final short blockStateId = stream.readShort();
|
||||
final short customBlockId = stream.readShort();
|
||||
|
||||
final boolean hasData = stream.readBoolean();
|
||||
@ -49,9 +49,9 @@ public class ChunkReader {
|
||||
}
|
||||
|
||||
if (customBlockId != 0) {
|
||||
chunkBatch.setSeparateBlocks(x, y, z, blockId, customBlockId, data);
|
||||
chunkBatch.setSeparateBlocks(x, y, z, blockStateId, customBlockId, data);
|
||||
} else {
|
||||
chunkBatch.setBlock(x, y, z, blockId, data);
|
||||
chunkBatch.setBlockStateId(x, y, z, blockStateId, data);
|
||||
}
|
||||
}
|
||||
} catch (EOFException e) {
|
||||
|
@ -21,13 +21,13 @@ public final class EntityUtils {
|
||||
if (!ent1.getInstance().equals(ent2.getInstance()))
|
||||
return false;
|
||||
|
||||
Chunk chunk = ent1.getInstance().getChunkAt(ent1.getPosition());
|
||||
final Chunk chunk = ent1.getInstance().getChunkAt(ent1.getPosition());
|
||||
|
||||
long[] visibleChunksEntity = ChunkUtils.getChunksInRange(ent2.getPosition(), MinecraftServer.ENTITY_VIEW_DISTANCE);
|
||||
for (long visibleChunk : visibleChunksEntity) {
|
||||
int[] chunkPos = ChunkUtils.getChunkCoord(visibleChunk);
|
||||
int chunkX = chunkPos[0];
|
||||
int chunkZ = chunkPos[1];
|
||||
final int[] chunkPos = ChunkUtils.getChunkCoord(visibleChunk);
|
||||
final int chunkX = chunkPos[0];
|
||||
final int chunkZ = chunkPos[1];
|
||||
if (chunk.getChunkX() == chunkX && chunk.getChunkZ() == chunkZ)
|
||||
return true;
|
||||
}
|
||||
@ -36,18 +36,17 @@ public final class EntityUtils {
|
||||
}
|
||||
|
||||
public static boolean isOnGround(Entity entity) {
|
||||
Instance instance = entity.getInstance();
|
||||
final Instance instance = entity.getInstance();
|
||||
if (instance == null)
|
||||
return false;
|
||||
|
||||
Position entityPosition = entity.getPosition();
|
||||
final Position entityPosition = entity.getPosition();
|
||||
|
||||
// TODO: check entire bounding box
|
||||
BlockPosition blockPosition = entityPosition.toBlockPosition();
|
||||
blockPosition = blockPosition.subtract(0, 1, 0);
|
||||
final BlockPosition blockPosition = entityPosition.toBlockPosition().subtract(0, 1, 0);
|
||||
try {
|
||||
short blockId = instance.getBlockId(blockPosition);
|
||||
Block block = Block.fromId(blockId);
|
||||
final short blockStateId = instance.getBlockStateId(blockPosition);
|
||||
final Block block = Block.fromStateId(blockStateId);
|
||||
return block.isSolid();
|
||||
} catch (NullPointerException e) {
|
||||
// Probably an entity at the border of an unloaded chunk
|
||||
|
Loading…
Reference in New Issue
Block a user