Skip to content

Commit

Permalink
一些小调整
Browse files Browse the repository at this point in the history
  • Loading branch information
wode490390 committed Mar 2, 2024
1 parent 01bbd13 commit 9f70ead
Show file tree
Hide file tree
Showing 15 changed files with 166 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ public void sendAttributes() {
Attribute.getAttribute(Attribute.PLAYER_EXHAUSTION).setValue(this.getFoodData().getExhaustionLevel()),
movementSpeedAttribute.copy().setValue(movementSpeedAttribute.getModifiedValue()),
Attribute.getAttribute(Attribute.PLAYER_LEVEL).setValue(this.getExperienceLevel()),
Attribute.getAttribute(Attribute.PLAYER_EXPERIENCE).setValue(((float) this.getExperience()) / calculateRequireExperience(this.getExperienceLevel()))
Attribute.getAttribute(Attribute.PLAYER_EXPERIENCE).setValue(Mth.clamp(((float) this.getExperience()) / calculateRequireExperience(this.getExperienceLevel()), 0, 1))
);
}

Expand Down
62 changes: 46 additions & 16 deletions src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipParameters;

import javax.annotation.Nullable;
import java.io.*;
Expand All @@ -92,6 +94,7 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.Deflater;

/**
* @author MagicDroidX
Expand Down Expand Up @@ -126,12 +129,16 @@ public class Server {
private long nextTick;

private final float[] tickAverage = {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20};
private float tpsAverage = 20;

private final float[] useAverage = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
private float tickUsageAverage;

private float maxTick = 20;
private float tps = 20;

private float maxUse = 0;
private float tickUsage;

private int sendUsageTicker = 0;

Expand Down Expand Up @@ -1207,7 +1214,9 @@ private boolean tick() {
this.titleTick();
this.network.resetStatistics();
this.maxTick = 20;
this.tps = 20;
this.maxUse = 0;
this.tickUsage = 0;

if ((this.tickCounter & 0b111111111) == 0) {
try {
Expand Down Expand Up @@ -1249,17 +1258,21 @@ private boolean tick() {

if (this.maxTick > tick) {
this.maxTick = tick;
this.tps = Math.round(this.maxTick * 100) / 100f;
}

if (this.maxUse < use) {
this.maxUse = use;
this.tickUsage = Math.round(this.maxUse * 100) / 100f;
}

System.arraycopy(this.tickAverage, 1, this.tickAverage, 0, this.tickAverage.length - 1);
this.tickAverage[this.tickAverage.length - 1] = tick;
this.tpsAverage = getAverage(this.tickAverage);

System.arraycopy(this.useAverage, 1, this.useAverage, 0, this.useAverage.length - 1);
this.useAverage[this.useAverage.length - 1] = use;
this.tickUsageAverage = getAverage(this.useAverage);

if (this.enableJmxMonitoring) {
long diffNano = nowNano - tickTimeNano;
Expand Down Expand Up @@ -1298,8 +1311,8 @@ public void titleTick() {
title += " | U " + NukkitMath.round((this.network.getUpload() / 1024 * 1000), 2)
+ " D " + NukkitMath.round((this.network.getDownload() / 1024 * 1000), 2) + " kB/s";
}
title += " | TPS " + this.getTicksPerSecond() +
" | Load " + this.getTickUsage() + "%" + (char) 0x07;
title += " | TPS " + this.getTicksPerSecondAverage() +
" | Load " + this.getTickUsageAverage() + "%" + (char) 0x07;

System.out.print(title);
}
Expand Down Expand Up @@ -1538,37 +1551,36 @@ public int getTick() {
}

public float getTicksPerSecond() {
return ((float) Math.round(this.maxTick * 100)) / 100;
return tps;
}

public float getTicksPerSecondRaw() {
return this.maxTick;
}

public float getTicksPerSecondAverage() {
float sum = 0;
int count = this.tickAverage.length;
for (float aTickAverage : this.tickAverage) {
sum += aTickAverage;
}
return (float) NukkitMath.round(sum / count, 2);
return tpsAverage;
}

public float getTickUsage() {
return (float) NukkitMath.round(this.maxUse * 100, 2);
return tickUsage;
}

public float getTickUsageRaw() {
return this.maxUse;
}

public float getTickUsageAverage() {
return tickUsageAverage;
}

private static float getAverage(float[] array) {
float sum = 0;
int count = this.useAverage.length;
for (float aUseAverage : this.useAverage) {
sum += aUseAverage;
int count = array.length;
for (float num : array) {
sum += num;
}
return ((float) Math.round(sum / count * 100)) / 100;
return Math.round(sum / count * 100) / 100f;
}

public SimpleCommandMap getCommandMap() {
Expand Down Expand Up @@ -1634,11 +1646,29 @@ public void saveOfflinePlayerData(String name, CompoundTag tag) {
this.saveOfflinePlayerData(name, tag, false);
}

private static final GzipParameters GZIP_PARAMETERS = new GzipParameters();

static {
GZIP_PARAMETERS.setCompressionLevel(Deflater.BEST_SPEED);
}

public void saveOfflinePlayerData(String name, CompoundTag tag, boolean async) {
if (this.shouldSavePlayerData()) {
try {
if (async) {
this.getScheduler().scheduleAsyncTask(null, new FileWriteTask(this.getDataPath() + "players/" + name.toLowerCase() + ".dat", NBTIO.writeGZIPCompressed(tag, ByteOrder.BIG_ENDIAN)));
// EC force async
if (true || async) {
byte[] bytes = NBTIO.write(tag, ByteOrder.BIG_ENDIAN);
this.getScheduler().scheduleAsyncTask(null, new FileWriteTask(this.getDataPath() + "players/" + name.toLowerCase() + ".dat", () -> {
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
if (bytes.length != 0) {
try (OutputStream os = new GzipCompressorOutputStream(baos, GZIP_PARAMETERS)) {
os.write(bytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return new ByteArrayInputStream(baos.toByteArray());
}));
} else {
Utils.writeFile(this.getDataPath() + "players/" + name.toLowerCase() + ".dat", new ByteArrayInputStream(NBTIO.writeGZIPCompressed(tag, ByteOrder.BIG_ENDIAN)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/cn/nukkit/block/BlockTNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void prime(int fuse, Entity source) {
return;
}

double mot = ThreadLocalRandom.current().nextDouble() * Math.PI * 2;
float mot = ThreadLocalRandom.current().nextFloat() * Mth.PI * 2;
CompoundTag nbt = Entity.getDefaultNBT(x + 0.5, y, z + 0.5, -Mth.sin(mot) * 0.02, 0.2, -Mth.cos(mot) * 0.02)
.putByte("Fuse", fuse);
Entity tnt = new EntityPrimedTNT(
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/cn/nukkit/command/defaults/DeveloperCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import cn.nukkit.command.exceptions.CommandExceptions;
import cn.nukkit.command.exceptions.CommandSyntaxException;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.lang.TranslationContainer;
import cn.nukkit.level.Position;
import cn.nukkit.network.protocol.EntityEventPacket;
Expand Down Expand Up @@ -98,6 +100,10 @@ public DeveloperCommand(String name) {
CommandParameter.newType("event", CommandParamType.INT),
CommandParameter.newType("data", true, CommandParamType.INT),
});
this.commandParameters.put("damage", new CommandParameter[]{
CommandParameter.newEnum("subCommand", new CommandEnum("SubCommandDamage", "damage")),
CommandParameter.newType("entity", true, CommandParamType.TARGET),
});
}

@Override
Expand Down Expand Up @@ -323,6 +329,63 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
}
return false;
}
case "damage": {
CommandParser parser = new CommandParser(this, sender, args);
try {
parser.literal();
List<Entity> entities = parser.parseTargetsOrSelf();

for (Entity entity : entities) {
EntityDamageByEntityEvent event0 = new EntityDamageByEntityEvent(entity, entity, DamageCause.PROJECTILE, 0);
if (entity.attack(event0)) {
sender.sendMessage("projectile 0 true : set -0 health, actual -0 health");
} else {
sender.sendMessage("projectile 0 false: set -0 health, actual -0 health");
}

EntityDamageByEntityEvent event1 = new EntityDamageByEntityEvent(entity, entity, DamageCause.ENTITY_ATTACK, 1);
if (entity.attack(event1)) {
sender.sendMessage("melee 1 true : set -1 health, actual -1 health (diff)");
} else {
sender.sendMessage("melee 1 false: set -1 health, actual -1 health (diff)");
}

EntityDamageByEntityEvent event2 = new EntityDamageByEntityEvent(entity, entity, DamageCause.ENTITY_ATTACK, 1);
if (entity.attack(event2)) {
sender.sendMessage("melee 2 true : set -1 health, actual -0 health (CD)");
} else {
sender.sendMessage("melee 2 false: set -1 health, actual -0 health (CD)");
}

EntityDamageByEntityEvent event3 = new EntityDamageByEntityEvent(entity, entity, DamageCause.ENTITY_ATTACK, 2);
if (entity.attack(event3)) {
sender.sendMessage("melee 3 true : set -2 health, actual -1 health (diff)");
} else {
sender.sendMessage("melee 3 false: set -2 health, actual -1 health (diff)");
}

EntityDamageByEntityEvent event4 = new EntityDamageByEntityEvent(entity, entity, DamageCause.ENTITY_ATTACK, 2);
if (entity.attack(event4)) {
sender.sendMessage("melee 4 true : set -2 health, actual -0 health (CD)");
} else {
sender.sendMessage("melee 4 false: set -2 health, actual -0 health (CD)");
}

EntityDamageByEntityEvent event5 = new EntityDamageByEntityEvent(entity, entity, DamageCause.ENTITY_ATTACK, 5);
if (entity.attack(event5)) {
sender.sendMessage("melee 5 true : set -5 health, actual -3 health (diff)");
} else {
sender.sendMessage("melee 5 false: set -5 health, actual -3 health (diff)");
}
}

sender.sendMessage("success");
return true;
} catch (CommandSyntaxException e) {
sender.sendMessage(parser.getErrorMessage());
}
return false;
}
}

sendUsage(sender);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/cn/nukkit/command/defaults/StatusCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)
sender.sendMessage(TextFormat.GOLD + "Uptime: " + formatUptime(time));

TextFormat tpsColor = TextFormat.GREEN;
float tps = server.getTicksPerSecond();
float tps = server.getTicksPerSecondAverage();
if (tps < 12) {
tpsColor = TextFormat.RED;
} else if (tps < 17) {
Expand All @@ -51,7 +51,7 @@ public boolean execute(CommandSender sender, String commandLabel, String[] args)

sender.sendMessage(TextFormat.GOLD + "Current TPS: " + tpsColor + NukkitMath.round(tps, 2));

sender.sendMessage(TextFormat.GOLD + "Load: " + tpsColor + server.getTickUsage() + "%");
sender.sendMessage(TextFormat.GOLD + "Load: " + tpsColor + server.getTickUsageAverage() + "%");

sender.sendMessage(TextFormat.GOLD + "Network upload: " + TextFormat.GREEN + NukkitMath.round((server.getNetwork().getUpload() / 1024 * 1000), 2) + " kB/s");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Item dispense(BlockDispenser block, BlockFace face, Item item) {
Random rand = ThreadLocalRandom.current();
Vector3 motion = new Vector3();

double offset = rand.nextDouble() * 0.1 + 0.2;
float offset = rand.nextFloat() * 0.1f + 0.2f;

motion.x = face.getXOffset() * offset;
motion.y = 0.20000000298023224;
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/cn/nukkit/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -1126,8 +1126,9 @@ public void sendData(Player[] players, EntityMetadata data) {
pk.eid = this.getId();
pk.metadata = data == null ? this.dataProperties : data;

if (this.dataProperties.exists(DATA_NUKKIT_FLAGS)) {
pk.metadata.put(this.dataProperties.get(DATA_NUKKIT_FLAGS));
EntityData<?> nukkitFlags = this.dataProperties.get(DATA_NUKKIT_FLAGS);
if (nukkitFlags != null) {
pk.metadata.put(nukkitFlags);
}

for (Player player : players) {
Expand Down Expand Up @@ -1303,7 +1304,7 @@ protected boolean checkObstruction(double x, double y, double z) {
direction = 5;
}

double force = ThreadLocalRandom.current().nextDouble() * 0.2 + 0.1;
float force = ThreadLocalRandom.current().nextFloat() * 0.2f + 0.1f;

if (direction == 0) {
this.motionX = -force;
Expand Down Expand Up @@ -2441,18 +2442,26 @@ public boolean setDataProperty(EntityData data, boolean send) {
}

this.dataProperties.put(data);

if (send) {
EntityMetadata metadata = new EntityMetadata();
metadata.put(this.dataProperties.get(data.getId()));
metadata.put(data);
if (data.getId() == DATA_FLAGS_EXTENDED) {
metadata.put(this.dataProperties.get(DATA_FLAGS));
EntityData<?> flags1 = this.dataProperties.get(DATA_FLAGS);
if (flags1 != null) {
metadata.put(flags1);
}
}
if (data.getId() == DATA_FLAGS) {
// for multi-version conversion
metadata.put(this.dataProperties.get(DATA_FLAGS_EXTENDED));
EntityData<?> flags2 = this.dataProperties.get(DATA_FLAGS_EXTENDED);
if (flags2 != null) {
metadata.put(flags2);
}
}
this.sendData(this.getViewers().values().toArray(new Player[0]), metadata);
}

return true;
}

Expand Down
15 changes: 8 additions & 7 deletions src/main/java/cn/nukkit/entity/data/EntityMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import lombok.ToString;

import javax.annotation.Nullable;

/**
* author: MagicDroidX
* Nukkit Project
Expand All @@ -18,19 +20,18 @@ public class EntityMetadata {

private final Int2ObjectMap<EntityData> map = new Int2ObjectOpenHashMap<>();

@Nullable
public EntityData get(int id) {
return this.getOrDefault(id, null);
}

public EntityData getOrDefault(int id, EntityData defaultValue) {
try {
return this.map.getOrDefault(id, defaultValue).setId(id);
} catch (Exception e) {
if (defaultValue != null) {
return defaultValue.setId(id);
}
@Nullable
public EntityData getOrDefault(int id, @Nullable EntityData defaultValue) {
EntityData<?> data = this.map.getOrDefault(id, defaultValue);
if (data == null) {
return null;
}
return data.setId(id);
}

public boolean exists(int id) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/cn/nukkit/entity/item/EntityFishingHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,19 @@ public void fishBites() {
Random random = ThreadLocalRandom.current();
for (int i = 0; i < 5; i++) {
this.level.addParticle(new BubbleParticle(this.setComponents(
this.x + random.nextDouble() * 0.5 - 0.25,
this.x + random.nextFloat() * 0.5f - 0.25f,
this.getWaterHeight(),
this.z + random.nextDouble() * 0.5 - 0.25
this.z + random.nextFloat() * 0.5f - 0.25f
)));
}
}

public void spawnFish() {
Random random = ThreadLocalRandom.current();
this.fish = new Vector3(
this.x + (random.nextDouble() * 1.2 + 1) * (random.nextBoolean() ? -1 : 1),
this.x + (random.nextFloat() * 1.2f + 1) * (random.nextBoolean() ? -1 : 1),
this.getWaterHeight(),
this.z + (random.nextDouble() * 1.2 + 1) * (random.nextBoolean() ? -1 : 1)
this.z + (random.nextFloat() * 1.2f + 1) * (random.nextBoolean() ? -1 : 1)
);
}

Expand Down
Loading

0 comments on commit 9f70ead

Please sign in to comment.