Skip to content

Commit

Permalink
Improve Performance of Void Netting
Browse files Browse the repository at this point in the history
or reduce chance of crashing the server with it
  • Loading branch information
Flo56958 committed Jun 4, 2020
1 parent 0df995b commit 618c23d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<artifactId>MineTinker-Core</artifactId>
<description>A TinkersConstruct (and other mods) inspired server plugin</description>
<url>https://flo56958.github.io/MineTinker</url>
<version>1.5</version>
<version>1.5.1</version>

<parent>
<groupId>de.flo56958</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,47 +144,54 @@ public void onDamage(EntityDamageEvent event) {
}
}

Location loc = player.getLocation();
for (int i = 0; i < level * radiusPerLevel; i++) {
for (int d = -i; d <= i; d++) {
int y = loc.getWorld().getHighestBlockYAt(loc.getBlockX() + d, loc.getBlockZ() + i - Math.abs(d));
if (y > 1) {
loc = new Location(loc.getWorld(), loc.getBlockX() + d, y + 2, loc.getBlockZ() + i - Math.abs(d));
break;
cooldownTracker.put(player.getUniqueId().toString(), time - Math.round(this.cooldownInSeconds * 0.95)); //Add small cooldown to improve server performance

Bukkit.getScheduler().runTaskAsynchronously(Main.getPlugin(), () -> { //run effect async as it does not need to stop all action on server if search takes to long
Location loc = player.getLocation();
for (int i = 0; i < level * radiusPerLevel; i++) {
for (int d = -i; d <= i; d++) {
int y = loc.getWorld().getHighestBlockYAt(loc.getBlockX() + d, loc.getBlockZ() + i - Math.abs(d));
if (y > 1) {
loc = new Location(loc.getWorld(), loc.getBlockX() + d, y + 2, loc.getBlockZ() + i - Math.abs(d));
break;
}
}
}
for (int d = -i + 1; d < i; d++) {
int y = loc.getWorld().getHighestBlockYAt(loc.getBlockX() + d, loc.getBlockZ() + Math.abs(d) - i);
if (y > 1) {
loc = new Location(loc.getWorld(), loc.getBlockX() + d, y + 2,loc.getBlockZ() + Math.abs(d) - i);
break;
for (int d = -i + 1; d < i; d++) {
int y = loc.getWorld().getHighestBlockYAt(loc.getBlockX() + d, loc.getBlockZ() + Math.abs(d) - i);
if (y > 1) {
loc = new Location(loc.getWorld(), loc.getBlockX() + d, y + 2,loc.getBlockZ() + Math.abs(d) - i);
break;
}
}
}
}

if (loc.equals(player.getLocation())) {
//No suitable place found
ChatWriter.logModifier(player, event, this, armor, "Could not find suitable Block to teleport!");
ChatWriter.sendActionBar(player, this.getName() + ": " + LanguageManager.getString("Modifier.Void-Netting.CouldNotFindBlock", player));
return;
}

Location oldLoc = player.getLocation().clone();
player.teleport(loc);
player.setVelocity(new Vector(0, 0.1, 0)); //Slow the fall
cooldownTracker.put(player.getUniqueId().toString(), time);
ChatWriter.logModifier(player, event, this, armor,
String.format("Location(%d/%d/%d -> %d/%d/%d)",
oldLoc.getBlockX(), oldLoc.getBlockY(), oldLoc.getBlockZ(),
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), String.format("Cooldown(%ds)", cooldownTime / 1000));

if (this.particles) {
loc.getWorld().spawnParticle(Particle.PORTAL, loc, 20);
loc.getWorld().spawnParticle(Particle.PORTAL, oldLoc, 20);
}
if (this.sound) {
player.getWorld().playSound(loc, Sound.ENTITY_ENDERMAN_TELEPORT, 1.0f, 1.0f);
player.getWorld().playSound(oldLoc, Sound.ENTITY_ENDERMAN_TELEPORT, 1.0f, 1.0f);
}
if (loc.equals(player.getLocation())) {
//No suitable place found
ChatWriter.logModifier(player, event, this, armor, "Could not find suitable Block to teleport!");
ChatWriter.sendActionBar(player, this.getName() + ": " + LanguageManager.getString("Modifier.Void-Netting.CouldNotFindBlock", player));
return;
}
Location oldLoc = player.getLocation().clone();
cooldownTracker.put(player.getUniqueId().toString(), time);
ChatWriter.logModifier(player, event, this, armor,
String.format("Location(%d/%d/%d -> %d/%d/%d)",
oldLoc.getBlockX(), oldLoc.getBlockY(), oldLoc.getBlockZ(),
loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()), String.format("Cooldown(%ds)", cooldownTime / 1000));

Location finalLoc = loc;
Bukkit.getScheduler().runTask(Main.getPlugin(), () -> { //Teleport needs to be in sync
player.teleport(finalLoc);
player.setVelocity(new Vector(0, 0.3, 0)); //Slow the fall

if (this.particles) {
finalLoc.getWorld().spawnParticle(Particle.PORTAL, finalLoc, 20);
finalLoc.getWorld().spawnParticle(Particle.PORTAL, oldLoc, 20);
}
if (this.sound) {
player.getWorld().playSound(finalLoc, Sound.ENTITY_ENDERMAN_TELEPORT, 1.0f, 1.0f);
player.getWorld().playSound(oldLoc, Sound.ENTITY_ENDERMAN_TELEPORT, 1.0f, 1.0f);
}
});
});
}
}

0 comments on commit 618c23d

Please sign in to comment.