mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-19 14:41:34 +01:00
Modified digging listener, added more clear exceptions and some comments
This commit is contained in:
parent
f97328a3bd
commit
f5ddc66c43
@ -212,7 +212,7 @@ public class PlayerInit {
|
||||
});
|
||||
|
||||
player.addEventCallback(PlayerSpawnEvent.class, event -> {
|
||||
player.setGameMode(GameMode.CREATIVE);
|
||||
player.setGameMode(GameMode.SURVIVAL);
|
||||
player.teleport(new Position(0, 45, 0));
|
||||
|
||||
player.setGlowing(true);
|
||||
|
@ -9,22 +9,15 @@ import net.minestom.server.instance.block.Block;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class ChunkGeneratorDemo extends ChunkGenerator {
|
||||
|
||||
private final Random random = new Random();
|
||||
|
||||
@Override
|
||||
public void generateChunkData(ChunkBatch batch, int chunkX, int chunkZ) {
|
||||
for (byte x = 0; x < Chunk.CHUNK_SIZE_X; x++)
|
||||
for (byte z = 0; z < Chunk.CHUNK_SIZE_Z; z++) {
|
||||
for (byte y = 0; y < 65; y++) {
|
||||
if (random.nextInt(100) > 10) {
|
||||
batch.setCustomBlock(x, y, z, "custom_block");
|
||||
} else {
|
||||
batch.setBlock(x, y, z, Block.DIAMOND_BLOCK);
|
||||
}
|
||||
batch.setBlock(x, y, z, Block.STONE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ public class BenchmarkManager {
|
||||
|
||||
private Map<Long, Long> lastCpuTimeMap = new HashMap<>();
|
||||
private Map<Long, Long> lastUserTimeMap = new HashMap<>();
|
||||
private Map<Long, Long> lastWaitedMap = new HashMap<>();
|
||||
private Map<Long, Long> lastBlockedMap = new HashMap<>();
|
||||
|
||||
private Map<String, ThreadResult> resultMap = new ConcurrentHashMap<>();
|
||||
@ -100,26 +101,30 @@ public class BenchmarkManager {
|
||||
|
||||
long lastCpuTime = lastCpuTimeMap.getOrDefault(id, 0L);
|
||||
long lastUserTime = lastUserTimeMap.getOrDefault(id, 0L);
|
||||
long lastWaitedTime = lastWaitedMap.getOrDefault(id, 0L);
|
||||
long lastBlockedTime = lastBlockedMap.getOrDefault(id, 0L);
|
||||
|
||||
long blockedTime = threadInfo2.getBlockedTime();
|
||||
//long waitedTime = threadInfo2.getWaitedTime();
|
||||
long waitedTime = threadInfo2.getWaitedTime();
|
||||
long cpuTime = threadMXBean.getThreadCpuTime(id);
|
||||
long userTime = threadMXBean.getThreadUserTime(id);
|
||||
|
||||
lastCpuTimeMap.put(id, cpuTime);
|
||||
lastUserTimeMap.put(id, userTime);
|
||||
lastWaitedMap.put(id, waitedTime);
|
||||
lastBlockedMap.put(id, blockedTime);
|
||||
|
||||
double totalCpuTime = (double) (cpuTime - lastCpuTime) / 1000000D;
|
||||
double totalUserTime = (double) (userTime - lastUserTime) / 1000000D;
|
||||
long totalBlocked = blockedTime - lastBlockedTime;
|
||||
long totalWaited = waitedTime - lastWaitedTime;
|
||||
|
||||
double cpuPercentage = totalCpuTime / (double) time * 100L;
|
||||
double userPercentage = totalUserTime / (double) time * 100L;
|
||||
double waitedPercentage = totalWaited / (double) time * 100L;
|
||||
double blockedPercentage = totalBlocked / (double) time * 100L;
|
||||
|
||||
ThreadResult threadResult = new ThreadResult(cpuPercentage, userPercentage, blockedPercentage);
|
||||
ThreadResult threadResult = new ThreadResult(cpuPercentage, userPercentage, waitedPercentage, blockedPercentage);
|
||||
resultMap.put(name, threadResult);
|
||||
}
|
||||
}
|
||||
|
@ -2,11 +2,12 @@ package net.minestom.server.benchmark;
|
||||
|
||||
public class ThreadResult {
|
||||
|
||||
private double cpuPercentage, userPercentage, blockedPercentage;
|
||||
private double cpuPercentage, userPercentage, waitedPercentage, blockedPercentage;
|
||||
|
||||
protected ThreadResult(double cpuPercentage, double userPercentage, double blockedPercentage) {
|
||||
protected ThreadResult(double cpuPercentage, double userPercentage, double waitedPercentage, double blockedPercentage) {
|
||||
this.cpuPercentage = cpuPercentage;
|
||||
this.userPercentage = userPercentage;
|
||||
this.waitedPercentage = waitedPercentage;
|
||||
this.blockedPercentage = blockedPercentage;
|
||||
}
|
||||
|
||||
@ -18,6 +19,10 @@ public class ThreadResult {
|
||||
return userPercentage;
|
||||
}
|
||||
|
||||
public double getWaitedPercentage() {
|
||||
return waitedPercentage;
|
||||
}
|
||||
|
||||
public double getBlockedPercentage() {
|
||||
return blockedPercentage;
|
||||
}
|
||||
|
@ -252,6 +252,9 @@ public class Player extends LivingEntity {
|
||||
|
||||
@Override
|
||||
public boolean damage(DamageType type, float value) {
|
||||
if (isInvulnerable())
|
||||
return false;
|
||||
|
||||
// Compute final heart based on health and additional hearts
|
||||
boolean result = super.damage(type, value);
|
||||
if (result) {
|
||||
@ -1340,6 +1343,9 @@ public class Player extends LivingEntity {
|
||||
triggerStatus(permissionLevelStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param reduced should the player has the reduced debug screen
|
||||
*/
|
||||
public void setReducedDebugScreenInformation(boolean reduced) {
|
||||
this.reducedDebugScreenInformation = reduced;
|
||||
|
||||
@ -1348,14 +1354,28 @@ public class Player extends LivingEntity {
|
||||
triggerStatus(debugScreenStatus);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the player has the reduced debug screen, false otherwise
|
||||
*/
|
||||
public boolean hasReducedDebugScreenInformation() {
|
||||
return reducedDebugScreenInformation;
|
||||
}
|
||||
|
||||
/**
|
||||
* The invulnerable field appear in the {@link PlayerAbilitiesPacket} packet
|
||||
*
|
||||
* @return true if the player is invulnerable, false otherwise
|
||||
*/
|
||||
public boolean isInvulnerable() {
|
||||
return invulnerable;
|
||||
}
|
||||
|
||||
/**
|
||||
* This do update the {@code invulnerable} field in the packet {@link PlayerAbilitiesPacket}
|
||||
* and prevent the player from receiving damage
|
||||
*
|
||||
* @param invulnerable should the player be invulnerable
|
||||
*/
|
||||
public void setInvulnerable(boolean invulnerable) {
|
||||
this.invulnerable = invulnerable;
|
||||
refreshAbilities();
|
||||
@ -1372,10 +1392,18 @@ public class Player extends LivingEntity {
|
||||
* @param flying should the player fly
|
||||
*/
|
||||
public void setFlying(boolean flying) {
|
||||
refreshFlying(flying);
|
||||
this.flying = flying;
|
||||
refreshAbilities();
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the internal flying field
|
||||
* <p>
|
||||
* Mostly unsafe since there is nothing to backup the value, used internally for creative players
|
||||
*
|
||||
* @param flying the new flying field
|
||||
* @see #setFlying(boolean) instead
|
||||
*/
|
||||
public void refreshFlying(boolean flying) {
|
||||
this.flying = flying;
|
||||
}
|
||||
@ -1412,10 +1440,18 @@ public class Player extends LivingEntity {
|
||||
refreshAbilities();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the flying speed of the player
|
||||
*/
|
||||
public float getFlyingSpeed() {
|
||||
return flyingSpeed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the internal field and send a {@link PlayerAbilitiesPacket} with the new flying speed
|
||||
*
|
||||
* @param flyingSpeed the new flying speed of the player
|
||||
*/
|
||||
public void setFlyingSpeed(float flyingSpeed) {
|
||||
this.flyingSpeed = flyingSpeed;
|
||||
refreshAbilities();
|
||||
|
@ -7,6 +7,7 @@ import net.minestom.server.instance.block.BlockManager;
|
||||
import net.minestom.server.instance.block.CustomBlock;
|
||||
import net.minestom.server.utils.BlockPosition;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
|
||||
public interface BlockModifier {
|
||||
|
||||
@ -53,6 +54,8 @@ public interface BlockModifier {
|
||||
|
||||
default void setCustomBlock(int x, int y, int z, String customBlockId, Data data) {
|
||||
CustomBlock customBlock = BLOCK_MANAGER.getCustomBlock(customBlockId);
|
||||
Check.notNull(customBlock, "The CustomBlock " + customBlockId + " is not registered");
|
||||
|
||||
setCustomBlock(x, y, z, customBlock.getCustomBlockId(), data);
|
||||
}
|
||||
|
||||
|
@ -24,6 +24,7 @@ import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import net.minestom.server.utils.player.PlayerUtils;
|
||||
import net.minestom.server.utils.time.TimeUnit;
|
||||
import net.minestom.server.utils.validate.Check;
|
||||
import net.minestom.server.world.Dimension;
|
||||
|
||||
import java.util.*;
|
||||
@ -360,6 +361,9 @@ public abstract class Instance implements BlockModifier, EventHandler, DataConta
|
||||
}
|
||||
|
||||
public void addEntityToChunk(Entity entity, Chunk chunk) {
|
||||
Check.notNull(chunk,
|
||||
"The chunk " + chunk + " is not loaded, you can make it automatic by using Instance#enableAutoChunkLoad(true)");
|
||||
Check.argCondition(!chunk.isLoaded(), "Chunk " + chunk + " has been unloaded previously");
|
||||
long chunkIndex = ChunkUtils.getChunkIndex(chunk.getChunkX(), chunk.getChunkZ());
|
||||
synchronized (chunkEntities) {
|
||||
Set<Entity> entities = getEntitiesInChunk(chunkIndex);
|
||||
|
@ -12,6 +12,7 @@ public class AbilitiesListener {
|
||||
|
||||
if (canFly) {
|
||||
boolean isFlying = (packet.flags & 0x2) > 0;
|
||||
|
||||
player.refreshFlying(isFlying);
|
||||
|
||||
if (isFlying) {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package net.minestom.server.listener;
|
||||
|
||||
import net.minestom.server.entity.GameMode;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.event.item.ItemUpdateStateEvent;
|
||||
import net.minestom.server.event.player.PlayerStartDiggingEvent;
|
||||
@ -29,45 +28,57 @@ public class PlayerDiggingListener {
|
||||
|
||||
Instance instance = player.getInstance();
|
||||
|
||||
if (instance == null)
|
||||
return;
|
||||
final short blockId = instance.getBlockId(blockPosition);
|
||||
|
||||
switch (status) {
|
||||
case STARTED_DIGGING:
|
||||
if (player.getGameMode() == GameMode.CREATIVE) {
|
||||
if (instance != null) {
|
||||
instance.breakBlock(player, blockPosition);
|
||||
}
|
||||
} else if (player.getGameMode() == GameMode.SURVIVAL) {
|
||||
if (instance != null) {
|
||||
CustomBlock customBlock = instance.getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
|
||||
if (customBlock != null) {
|
||||
int breakTime = customBlock.getBreakDelay(player, blockPosition);
|
||||
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 {
|
||||
if (Block.fromId(instance.getBlockId(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ())).breaksInstantaneously()) {
|
||||
if (player.getCustomBlockTarget() != null) {
|
||||
player.resetTargetBlock();
|
||||
removeEffect(player);
|
||||
}
|
||||
instance.breakBlock(player, blockPosition);
|
||||
final boolean instantBreak = player.isCreative() ||
|
||||
player.isInstantBreak() ||
|
||||
Block.fromId(blockId).breaksInstantaneously();
|
||||
|
||||
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
|
||||
ClientPlayerDiggingPacket.Status.FINISHED_DIGGING, true);
|
||||
} else {
|
||||
player.resetTargetBlock();
|
||||
removeEffect(player);
|
||||
}
|
||||
if (instantBreak) {
|
||||
instance.breakBlock(player, blockPosition);
|
||||
|
||||
if (!player.isCreative()) {
|
||||
if (player.getCustomBlockTarget() != null) {
|
||||
player.resetTargetBlock();
|
||||
removeEffect(player);
|
||||
}
|
||||
|
||||
sendAcknowledgePacket(player, blockPosition, blockId,
|
||||
ClientPlayerDiggingPacket.Status.FINISHED_DIGGING, true);
|
||||
}
|
||||
} else {
|
||||
CustomBlock customBlock = instance.getCustomBlock(blockPosition.getX(), blockPosition.getY(), blockPosition.getZ());
|
||||
if (customBlock != null) {
|
||||
int breakTime = customBlock.getBreakDelay(player, blockPosition);
|
||||
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 {
|
||||
if (player.getCustomBlockTarget() != null) {
|
||||
player.resetTargetBlock();
|
||||
removeEffect(player);
|
||||
}
|
||||
|
||||
instance.breakBlock(player, blockPosition);
|
||||
|
||||
sendAcknowledgePacket(player, blockPosition, customBlock.getBlockId(),
|
||||
ClientPlayerDiggingPacket.Status.FINISHED_DIGGING, true);
|
||||
}
|
||||
} else {
|
||||
if (player.getCustomBlockTarget() != null) {
|
||||
player.resetTargetBlock();
|
||||
removeEffect(player);
|
||||
}
|
||||
@ -78,7 +89,6 @@ public class PlayerDiggingListener {
|
||||
player.resetTargetBlock();
|
||||
removeEffect(player);
|
||||
|
||||
final short blockId = instance.getBlockId(blockPosition);
|
||||
sendAcknowledgePacket(player, blockPosition, blockId,
|
||||
ClientPlayerDiggingPacket.Status.CANCELLED_DIGGING, true);
|
||||
break;
|
||||
@ -87,12 +97,11 @@ public class PlayerDiggingListener {
|
||||
player.resetTargetBlock();
|
||||
removeEffect(player);
|
||||
} else {
|
||||
final short id = instance.getBlockId(blockPosition);
|
||||
if (instance != null) {
|
||||
instance.breakBlock(player, blockPosition);
|
||||
}
|
||||
|
||||
sendAcknowledgePacket(player, blockPosition, id,
|
||||
sendAcknowledgePacket(player, blockPosition, blockId,
|
||||
ClientPlayerDiggingPacket.Status.FINISHED_DIGGING, true);
|
||||
}
|
||||
break;
|
||||
|
Loading…
Reference in New Issue
Block a user