Added CustomBlock#getCustomBlockId, it is now defined by the developer and not increased automatically by a counter

This commit is contained in:
Felix Cravic 2020-04-27 23:13:17 +02:00
parent 22511ca052
commit ce40627a36
6 changed files with 22 additions and 14 deletions

View File

@ -37,4 +37,9 @@ public class StoneBlock extends CustomBlock {
public int getBreakDelay(Player player) {
return 750;
}
@Override
public short getCustomBlockId() {
return 2;
}
}

View File

@ -45,4 +45,9 @@ public class UpdatableBlockDemo extends CustomBlock {
public int getBreakDelay(Player player) {
return 500;
}
@Override
public short getCustomBlockId() {
return 1;
}
}

View File

@ -47,7 +47,7 @@ public interface BlockModifier {
default void setCustomBlock(int x, int y, int z, String customBlockId, Data data) {
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
setCustomBlock(x, y, z, customBlock.getId(), data);
setCustomBlock(x, y, z, customBlock.getCustomBlockId(), data);
}
default void setCustomBlock(int x, int y, int z, String customBlockId) {

View File

@ -80,7 +80,7 @@ public class Chunk implements Viewable {
private void setCustomBlock(int x, int y, int z, CustomBlock customBlock, Data data) {
UpdateConsumer updateConsumer = customBlock.hasUpdate() ? customBlock::update : null;
setBlock(x, y, z, customBlock.getBlockId(), customBlock.getId(), data, updateConsumer);
setBlock(x, y, z, customBlock.getBlockId(), customBlock.getCustomBlockId(), data, updateConsumer);
}
private void setBlock(int x, int y, int z, short blockId, short customId, Data data, UpdateConsumer updateConsumer) {
@ -183,7 +183,7 @@ public class Chunk implements Viewable {
protected void refreshBlockValue(int x, int y, int z, short blockId) {
CustomBlock customBlock = getCustomBlock(x, y, z);
short customBlockId = customBlock == null ? 0 : customBlock.getId();
short customBlockId = customBlock == null ? 0 : customBlock.getCustomBlockId();
refreshBlockValue(x, y, z, blockId, customBlockId);
}

View File

@ -16,7 +16,7 @@ public class BlockManager {
public void registerCustomBlock(CustomBlock customBlock) {
String identifier = customBlock.getIdentifier();
short id = customBlock.getId();
short id = customBlock.getCustomBlockId();
this.customBlocksInternalId.put(id, customBlock);
this.customBlocksId.put(identifier, customBlock);
}

View File

@ -6,24 +6,18 @@ import net.minestom.server.instance.Instance;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.time.UpdateOption;
import java.util.concurrent.atomic.AtomicInteger;
/**
* TODO
* - option to set the global as "global breaking" meaning that multiple players mining the same block will break it faster (cumulation)
*/
public abstract class CustomBlock {
private static final AtomicInteger idCounter = new AtomicInteger();
private short blockId;
private String identifier;
private short id;
public CustomBlock(short blockId, String identifier) {
this.blockId = blockId;
this.identifier = identifier;
this.id = (short) idCounter.incrementAndGet();
}
// TODO add another object parameter which will offer a lot of integrated features (like break animation, id change etc...)
@ -39,6 +33,14 @@ public abstract class CustomBlock {
public abstract UpdateOption getUpdateOption();
/**
* This id can be serialized in chunk file, meaning no duplicate should exist
* Changing this value halfway should mean potentially breaking the world
*
* @return the custom block id
*/
public abstract short getCustomBlockId();
/*
Time in ms
*/
@ -59,8 +61,4 @@ public abstract class CustomBlock {
public String getIdentifier() {
return identifier;
}
public short getId() {
return id;
}
}