Allow for a different visual block to be placed along a custom block

This commit is contained in:
jglrxavpok 2020-04-29 00:42:18 +02:00
parent 365cdf7818
commit 2292261de7
8 changed files with 60 additions and 23 deletions

View File

@ -16,6 +16,12 @@ public interface BlockModifier {
void setCustomBlock(int x, int y, int z, short customBlockId, 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) { default void setBlock(int x, int y, int z, short blockId) {
setBlock(x, y, z, blockId, null); setBlock(x, y, z, blockId, null);
} }

View File

@ -71,17 +71,17 @@ public class Chunk implements Viewable {
setBlock(x, y, z, blockId, (short) 0, data, null); setBlock(x, y, z, blockId, (short) 0, data, null);
} }
public void UNSAFE_setCustomBlock(int x, int y, int z, short customBlockId, Data data) { public void UNSAFE_setCustomBlock(int x, int y, int z, short visualBlockId, short customBlockId, Data data) {
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
if (customBlock == null) if (customBlock == null)
throw new IllegalArgumentException("The custom block " + customBlockId + " does not exist or isn't registered"); throw new IllegalArgumentException("The custom block " + customBlockId + " does not exist or isn't registered");
UNSAFE_setCustomBlock(x, y, z, customBlock, data); UNSAFE_setCustomBlock(x, y, z, visualBlockId, customBlock, data);
} }
protected void UNSAFE_setCustomBlock(int x, int y, int z, CustomBlock customBlock, Data data) { protected void UNSAFE_setCustomBlock(int x, int y, int z, short visualBlockId, CustomBlock customBlock, Data data) {
UpdateConsumer updateConsumer = customBlock.hasUpdate() ? customBlock::update : null; UpdateConsumer updateConsumer = customBlock.hasUpdate() ? customBlock::update : null;
setBlock(x, y, z, customBlock.getBlockId(), customBlock.getCustomBlockId(), data, updateConsumer); setBlock(x, y, z, visualBlockId, customBlock.getCustomBlockId(), data, updateConsumer);
} }
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 blockId, short customId, Data data, UpdateConsumer updateConsumer) {

View File

@ -352,4 +352,5 @@ public abstract class Instance implements BlockModifier, DataContainer {
Set<Entity> entities = chunkEntities.get(index); Set<Entity> entities = chunkEntities.get(index);
return entities != null ? entities : new CopyOnWriteArraySet<>(); return entities != null ? entities : new CopyOnWriteArraySet<>();
} }
} }

View File

@ -53,7 +53,13 @@ public class InstanceContainer extends Instance {
@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) {
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId); CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
setBlock(x, y, z, (short) 0, customBlock, data); setBlock(x, y, z, customBlock.getBlockId(), customBlock, data);
}
@Override
public void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
setBlock(x, y, z, blockId, 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 blockId, CustomBlock customBlock, Data data) {
@ -61,7 +67,6 @@ public class InstanceContainer extends Instance {
synchronized (chunk) { synchronized (chunk) {
boolean isCustomBlock = customBlock != null; boolean isCustomBlock = customBlock != null;
blockId = isCustomBlock ? customBlock.getBlockId() : blockId;
int index = SerializerUtils.coordToChunkIndex(x, y, z); int index = SerializerUtils.coordToChunkIndex(x, y, z);
@ -76,7 +81,7 @@ public class InstanceContainer extends Instance {
// 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, customBlock, data); chunk.UNSAFE_setCustomBlock(x, y, z, blockId, customBlock, data);
} else { } else {
chunk.UNSAFE_setBlock(x, y, z, blockId, data); chunk.UNSAFE_setBlock(x, y, z, blockId, data);
} }

View File

@ -149,6 +149,11 @@ public class SharedInstance extends Instance {
instanceContainer.setBlock(x, y, z, customBlockId, data); instanceContainer.setBlock(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 InstanceContainer getInstanceContainer() { public InstanceContainer getInstanceContainer() {
return instanceContainer; return instanceContainer;
} }

View File

@ -3,6 +3,7 @@ package net.minestom.server.instance.batch;
import net.minestom.server.data.Data; import net.minestom.server.data.Data;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.InstanceContainer; import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.block.CustomBlock;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -23,7 +24,7 @@ public class BlockBatch implements InstanceBatch {
public void setBlock(int x, int y, int z, short blockId, Data data) { public void setBlock(int x, int y, int z, short blockId, Data data) {
synchronized (this) { synchronized (this) {
Chunk chunk = this.instance.getChunkAt(x, z); Chunk chunk = this.instance.getChunkAt(x, z);
addBlockData(chunk, x, y, z, false, blockId, data); addBlockData(chunk, x, y, z, false, blockId, (short) 0, data);
} }
} }
@ -31,11 +32,20 @@ public class BlockBatch implements InstanceBatch {
public void setCustomBlock(int x, int y, int z, short blockId, Data data) { public void setCustomBlock(int x, int y, int z, short blockId, Data data) {
synchronized (this) { synchronized (this) {
Chunk chunk = this.instance.getChunkAt(x, z); Chunk chunk = this.instance.getChunkAt(x, z);
addBlockData(chunk, x, y, z, true, blockId, data); CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(blockId);
addBlockData(chunk, x, y, z, true, customBlock.getBlockId(), blockId, data);
} }
} }
private void addBlockData(Chunk chunk, int x, int y, int z, boolean customBlock, short blockId, Data data) { @Override
public void setSeparateBlocks(int x, int y, int z, short blockId, short customBlockId, Data data) {
synchronized (this) {
Chunk chunk = this.instance.getChunkAt(x, z);
addBlockData(chunk, x, y, z, true, blockId, customBlockId, data);
}
}
private void addBlockData(Chunk chunk, int x, int y, int z, boolean customBlock, short blockId, 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<>();
@ -44,8 +54,9 @@ public class BlockBatch implements InstanceBatch {
blockData.x = x; blockData.x = x;
blockData.y = y; blockData.y = y;
blockData.z = z; blockData.z = z;
blockData.isCustomBlock = customBlock; blockData.hasCustomBlock = customBlock;
blockData.blockId = blockId; blockData.blockId = blockId;
blockData.customBlockId = customBlockId;
blockData.data = data; blockData.data = data;
blocksData.add(blockData); blocksData.add(blockData);
@ -83,15 +94,16 @@ public class BlockBatch implements InstanceBatch {
private class BlockData { private class BlockData {
private int x, y, z; private int x, y, z;
private boolean isCustomBlock; private boolean hasCustomBlock;
private short blockId; private short blockId;
private short customBlockId;
private Data data; private Data data;
public void apply(Chunk chunk) { public void apply(Chunk chunk) {
if (!isCustomBlock) { if (!hasCustomBlock) {
chunk.UNSAFE_setBlock(x, y, z, blockId, data); chunk.UNSAFE_setBlock(x, y, z, blockId, data);
} else { } else {
chunk.UNSAFE_setCustomBlock(x, y, z, blockId, data); chunk.UNSAFE_setCustomBlock(x, y, z, blockId, customBlockId, data);
} }
} }

View File

@ -4,6 +4,7 @@ import net.minestom.server.data.Data;
import net.minestom.server.instance.Chunk; import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.ChunkGenerator; import net.minestom.server.instance.ChunkGenerator;
import net.minestom.server.instance.InstanceContainer; import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.block.CustomBlock;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -30,21 +31,28 @@ public class ChunkBatch implements InstanceBatch {
@Override @Override
public void setBlock(int x, int y, int z, short blockId, Data data) { public void setBlock(int x, int y, int z, short blockId, Data data) {
addBlockData((byte) x, y, (byte) z, false, blockId, data); addBlockData((byte) x, y, (byte) z, false, blockId, (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 blockId, Data data) {
addBlockData((byte) x, y, (byte) z, true, blockId, data); CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(blockId);
addBlockData((byte) x, y, (byte) z, true, customBlock.getBlockId(), blockId, data);
} }
private void addBlockData(byte x, int y, byte z, boolean customBlock, short blockId, Data 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);
}
private void addBlockData(byte x, int y, byte z, boolean customBlock, short blockId, short customBlockId, Data data) {
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.isCustomBlock = customBlock; blockData.hasCustomBlock = customBlock;
blockData.blockId = blockId; blockData.blockId = blockId;
blockData.customBlockId = customBlockId;
blockData.data = data; blockData.data = data;
this.dataList.add(blockData); this.dataList.add(blockData);
@ -81,15 +89,16 @@ public class ChunkBatch implements InstanceBatch {
private class BlockData { private class BlockData {
private int x, y, z; private int x, y, z;
private boolean isCustomBlock; private boolean hasCustomBlock;
private short blockId; private short blockId;
private short customBlockId;
private Data data; private Data data;
public void apply(Chunk chunk) { public void apply(Chunk chunk) {
if (!isCustomBlock) { if (!hasCustomBlock) {
chunk.UNSAFE_setBlock(x, y, z, blockId, data); chunk.UNSAFE_setBlock(x, y, z, blockId, data);
} else { } else {
chunk.UNSAFE_setCustomBlock(x, y, z, blockId, data); chunk.UNSAFE_setCustomBlock(x, y, z, blockId, customBlockId, data);
} }
} }

View File

@ -100,8 +100,7 @@ public class BlockPlacementListener {
if (!playerBlockPlaceEvent.isCancelled() && canPlace) { if (!playerBlockPlaceEvent.isCancelled() && canPlace) {
short customBlockId = playerBlockPlaceEvent.getCustomBlockId(); short customBlockId = playerBlockPlaceEvent.getCustomBlockId();
if(customBlockId != 0) { if(customBlockId != 0) {
instance.setCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getCustomBlockId()); instance.setSeparateBlocks(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockId(), playerBlockPlaceEvent.getCustomBlockId());
instance.refreshBlockId(blockPosition, playerBlockPlaceEvent.getBlockId());
} else { } else {
instance.setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockId()); instance.setBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ(), playerBlockPlaceEvent.getBlockId());
} }