Added a way to change CustomBlock blockId just after placement + more BlockAlternative tools

This commit is contained in:
Felix Cravic 2020-04-28 23:38:44 +02:00
parent a06274f877
commit d0c523f0e5
8 changed files with 50 additions and 23 deletions

View File

@ -122,6 +122,14 @@ public class PlayerInit {
});
player.addEventCallback(PlayerBlockInteractEvent.class, event -> {
if (event.getHand() != Player.Hand.MAIN)
return;
short blockId = player.getInstance().getBlockId(event.getBlockPosition());
player.sendMessage("block alternative id: " + Block.getBlockAlternative(blockId).getId());
});
player.addEventCallback(PickupItemEvent.class, event -> {
event.setCancelled(!player.getInventory().addItemStack(event.getItemStack())); // Cancel event if player does not have enough inventory space
});
@ -163,7 +171,7 @@ public class PlayerInit {
player.openInventory(inventory);
player.getInventory().addItemStack(new ItemStack((short) 1, (byte) 100));
player.getInventory().addItemStack(new ItemStack(Material.STONE, (byte) 100));
player.getInventory().addItemStack(new ItemStack(Material.DIAMOND_CHESTPLATE, (byte) 1));
/*TeamManager teamManager = Main.getTeamManager();

View File

@ -6,7 +6,6 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.CustomBlock;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.time.UpdateOption;
public class StoneBlock extends CustomBlock {
@ -17,6 +16,7 @@ public class StoneBlock extends CustomBlock {
@Override
public void onPlace(Instance instance, BlockPosition blockPosition, Data data) {
System.out.println("PLACED");
instance.refreshBlockId(blockPosition, Block.LAVA);
}
@Override
@ -29,11 +29,6 @@ public class StoneBlock extends CustomBlock {
return false;
}
@Override
public UpdateOption getUpdateOption() {
return null;
}
@Override
public int getBreakDelay(Player player) {
return 750;

View File

@ -29,7 +29,7 @@ public class NoiseTestGenerator extends ChunkGenerator {
if (random.nextInt(100) > 10) {
batch.setCustomBlock(x, y, z, "custom_block");
} else {
batch.setBlock(x, y, z, Block.DIAMOND_ORE);
batch.setBlock(x, y, z, Block.DIAMOND_BLOCK);
}
}
}

View File

@ -47,7 +47,7 @@ public abstract class Instance implements BlockModifier, DataContainer {
this.dimension = dimension;
}
public abstract void refreshBlockId(int x, int y, int z, short blockId);
public abstract void refreshBlockId(BlockPosition blockPosition, short blockId);
// Used to call BlockBreakEvent and sending particle packet if true
public abstract void breakBlock(Player player, BlockPosition blockPosition);
@ -153,10 +153,18 @@ public abstract class Instance implements BlockModifier, DataContainer {
return Collections.unmodifiableSet(getEntitiesInChunk(ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ())));
}
public void refreshBlockId(int x, int y, int z, short blockId) {
refreshBlockId(new BlockPosition(x, y, z), blockId);
}
public void refreshBlockId(int x, int y, int z, Block block) {
refreshBlockId(x, y, z, block.getBlockId());
}
public void refreshBlockId(BlockPosition blockPosition, Block block) {
refreshBlockId(blockPosition, block.getBlockId());
}
public void loadChunk(int chunkX, int chunkZ) {
loadChunk(chunkX, chunkZ, null);
}

View File

@ -84,22 +84,22 @@ public class InstanceContainer extends Instance {
// Refresh neighbors since a new block has been placed
executeNeighboursBlockPlacementRule(blockPosition);
// Refresh player chunk block
sendBlockChange(chunk, blockPosition, blockId);
// Call the place listener for custom block
if (isCustomBlock)
callBlockPlace(chunk, index, blockPosition);
// Refresh player chunk block
sendBlockChange(chunk, blockPosition, blockId);
}
}
@Override
public void refreshBlockId(int x, int y, int z, short blockId) {
Chunk chunk = getChunkAt(x, z);
public void refreshBlockId(BlockPosition blockPosition, short blockId) {
Chunk chunk = getChunkAt(blockPosition.getX(), blockPosition.getZ());
synchronized (chunk) {
chunk.refreshBlockValue(x, y, z, blockId);
chunk.refreshBlockValue(blockPosition.getX(), blockPosition.getY(),
blockPosition.getZ(), blockId);
BlockPosition blockPosition = new BlockPosition(x, y, z);
sendBlockChange(chunk, blockPosition, blockId);
}
}

View File

@ -25,8 +25,8 @@ public class SharedInstance extends Instance {
}
@Override
public void refreshBlockId(int x, int y, int z, short blockId) {
instanceContainer.refreshBlockId(x, y, z, blockId);
public void refreshBlockId(BlockPosition blockPosition, short blockId) {
instanceContainer.refreshBlockId(blockPosition, blockId);
}
@Override

View File

@ -690,12 +690,24 @@ public enum Block {
HONEY_BLOCK,
HONEYCOMB_BLOCK;
// Used to get a Block from any block alternatives id
private static Short2ObjectOpenHashMap<Block> blocksMap = new Short2ObjectOpenHashMap<>();
// Used to retrieve the appropriate BlockAlternative from any block id
private static Short2ObjectOpenHashMap<BlockAlternative> blocksAlternativesMap = new Short2ObjectOpenHashMap<>();
public static Block fromId(short blockId) {
return blocksMap.getOrDefault(blockId, AIR);
}
public static BlockAlternative getBlockAlternative(short blockId) {
return blocksAlternativesMap.get(blockId);
}
public static void registerBlockAlternative(BlockAlternative blockAlternative) {
blocksAlternativesMap.put(blockAlternative.id, blockAlternative);
}
private short blockId;
private List<BlockAlternative> blockAlternatives = new ArrayList<>();
@ -703,9 +715,9 @@ public enum Block {
this.blockId = blockId;
}
public void addBlockAlternative(short id, String... properties) {
this.blockAlternatives.add(new BlockAlternative(id, properties));
blocksMap.put(id, this);
public void addBlockAlternative(BlockAlternative blockAlternative) {
this.blockAlternatives.add(blockAlternative);
blocksMap.put(blockAlternative.id, this);
}
public short withProperties(String... properties) {
@ -1362,7 +1374,7 @@ public enum Block {
private short id;
private String[] properties;
protected BlockAlternative(short id, String... properties) {
public BlockAlternative(short id, String... properties) {
this.id = id;
this.properties = properties;
}

View File

@ -53,7 +53,11 @@ public class RegistryMain {
for (RegistryBlock.BlockState blockState : registryBlock.states) {
short id = blockState.id;
String[] properties = blockState.propertiesValues.toArray(new String[blockState.propertiesValues.size()]);
block.addBlockAlternative(id, properties);
Block.BlockAlternative blockAlternative = new Block.BlockAlternative(id, properties);
block.addBlockAlternative(blockAlternative);
Block.registerBlockAlternative(blockAlternative);
}
}
}