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