CustomBlock#getBreakDelay is now optional

This commit is contained in:
Felix Cravic 2020-04-28 17:58:34 +02:00
parent b128074c75
commit 6d9ac0b35d
4 changed files with 32 additions and 20 deletions

View File

@ -108,7 +108,7 @@ public class PlayerInit {
if (event.getHand() != Player.Hand.MAIN)
return;
if(event.getBlockId() == Block.STONE.getBlockId()) {
if (event.getBlockId() == Block.STONE.getBlockId()) {
event.setCustomBlockId((short) 2); // custom stone block
}

View File

@ -73,6 +73,7 @@ public class Player extends LivingEntity {
private BlockPosition targetBlockPosition;
private long targetBlockTime;
private byte targetLastStage;
private int timeBreak;
private Set<BossBar> bossBars = new CopyOnWriteArraySet<>();
private Team team;
@ -141,7 +142,6 @@ public class Player extends LivingEntity {
// Target block stage
if (targetCustomBlock != null) {
int timeBreak = targetCustomBlock.getBreakDelay(this);
int animationCount = 10;
long since = System.currentTimeMillis() - targetBlockTime;
byte stage = (byte) (since / (timeBreak / animationCount));
@ -429,11 +429,12 @@ public class Player extends LivingEntity {
/**
* Plays a given effect at the given position for this player
* @param effect the effect to play
* @param x x position of the effect
* @param y y position of the effect
* @param z z position of the effect
* @param data data for the effect
*
* @param effect the effect to play
* @param x x position of the effect
* @param y y position of the effect
* @param z z position of the effect
* @param data data for the effect
* @param disableRelativeVolume disable volume scaling based on distance
*/
public void playEffect(Effects effect, int x, int y, int z, int data, boolean disableRelativeVolume) {
@ -959,10 +960,11 @@ public class Player extends LivingEntity {
this.openInventory = openInventory;
}
public void refreshTargetBlock(CustomBlock targetCustomBlock, BlockPosition targetBlockPosition) {
public void refreshTargetBlock(CustomBlock targetCustomBlock, BlockPosition targetBlockPosition, int breakTime) {
this.targetCustomBlock = targetCustomBlock;
this.targetBlockPosition = targetBlockPosition;
this.targetBlockTime = targetBlockPosition == null ? 0 : System.currentTimeMillis();
this.timeBreak = breakTime;
}
public void resetTargetBlock() {

View File

@ -45,8 +45,12 @@ public abstract class CustomBlock {
*/
public abstract short getCustomBlockId();
/*
Time in ms
/**
* Called at digging start to check for custom breaking time
* Can be set to < 0 to be cancelled, in this case vanilla time will be used
*
* @param player the player who is trying to break the block
* @return the time in ms to break it
*/
public abstract int getBreakDelay(Player player);

View File

@ -37,17 +37,23 @@ public class PlayerDiggingListener {
if (instance != null) {
CustomBlock customBlock = instance.getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
if (customBlock != null) {
PlayerStartDiggingEvent playerStartDiggingEvent = new PlayerStartDiggingEvent(blockPosition, customBlock);
player.callEvent(PlayerStartDiggingEvent.class, playerStartDiggingEvent);
if (!playerStartDiggingEvent.isCancelled()) {
player.refreshTargetBlock(customBlock, blockPosition);
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, true);
int breakTime = customBlock.getBreakDelay(player);
if (breakTime >= 0) {
PlayerStartDiggingEvent playerStartDiggingEvent = new PlayerStartDiggingEvent(blockPosition, customBlock);
player.callEvent(PlayerStartDiggingEvent.class, playerStartDiggingEvent);
if (!playerStartDiggingEvent.isCancelled()) {
player.refreshTargetBlock(customBlock, blockPosition, breakTime);
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, true);
} else {
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false);
}
addEffect(player);
} else {
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
ClientPlayerDiggingPacket.Status.STARTED_DIGGING, false);
player.resetTargetBlock();
removeEffect(player);
}
addEffect(player);
} else {
player.resetTargetBlock();
removeEffect(player);
@ -96,7 +102,7 @@ public class PlayerDiggingListener {
ItemUpdateStateEvent itemUpdateStateEvent = new ItemUpdateStateEvent(updatedItem);
player.callEvent(ItemUpdateStateEvent.class, itemUpdateStateEvent);
player.refreshActiveHand(itemUpdateStateEvent.hasHandAnimation(), isOffhand, false);
player.sendPacketToViewers(player.getMetadataPacket());
break;