Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slight refactor #348

Merged
merged 8 commits into from Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
<executions>
<execution>
<id>checkstyle</id>
Expand Down
16 changes: 2 additions & 14 deletions src/main/java/pw/kaboom/extras/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
import pw.kaboom.extras.modules.entity.EntityExplosion;
import pw.kaboom.extras.modules.entity.EntityKnockback;
import pw.kaboom.extras.modules.entity.EntitySpawn;
import pw.kaboom.extras.modules.entity.EntityTeleport;
import pw.kaboom.extras.modules.player.*;
import pw.kaboom.extras.modules.server.ServerCommand;
import pw.kaboom.extras.modules.server.ServerGameRule;
import pw.kaboom.extras.modules.server.ServerTabComplete;
import pw.kaboom.extras.modules.server.ServerTick;

import java.io.File;
import java.util.Collections;
Expand All @@ -28,16 +26,6 @@ public final class Main extends JavaPlugin {

@Override
public void onLoad() {
/* Fill lists */
Collections.addAll(
BlockPhysics.getBlockFaces(),
BlockFace.NORTH,
BlockFace.SOUTH,
BlockFace.WEST,
BlockFace.EAST,
BlockFace.UP
);

/* Load missing config.yml defaults */
getConfig().options().copyDefaults(true);
saveConfig();
Expand Down Expand Up @@ -80,7 +68,6 @@ public void onEnable() {
this.getServer().getPluginManager().registerEvents(new EntityExplosion(), this);
this.getServer().getPluginManager().registerEvents(new EntityKnockback(), this);
this.getServer().getPluginManager().registerEvents(new EntitySpawn(), this);
this.getServer().getPluginManager().registerEvents(new EntityTeleport(), this);

/* Player-related modules */
this.getServer().getPluginManager().registerEvents(new PlayerChat(), this);
Expand All @@ -93,10 +80,11 @@ public void onEnable() {
this.getServer().getPluginManager().registerEvents(new PlayerPrefix(), this);

/* Server-related modules */
ServerGameRule.init(this);

this.getServer().getPluginManager().registerEvents(new ServerCommand(), this);
this.getServer().getPluginManager().registerEvents(new ServerGameRule(), this);
this.getServer().getPluginManager().registerEvents(new ServerTabComplete(), this);
this.getServer().getPluginManager().registerEvents(new ServerTick(), this);

/* Custom worlds */
this.getServer().createWorld(
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/pw/kaboom/extras/commands/CommandConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import pw.kaboom.extras.util.Utility;

import javax.annotation.Nonnull;

Expand All @@ -24,8 +24,7 @@ public boolean onCommand(final @Nonnull CommandSender sender,

Bukkit.dispatchCommand(
Bukkit.getConsoleSender(),
"minecraft:say " + ChatColor.translateAlternateColorCodes(
'&', String.join(" ", args))
"minecraft:say " + Utility.translateLegacyColors(String.join(" ", args))
);
return true;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/pw/kaboom/extras/commands/CommandUsername.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import pw.kaboom.extras.util.Utility;

import javax.annotation.Nonnull;
import java.util.HashMap;
Expand All @@ -28,8 +28,7 @@ public boolean onCommand(final @Nonnull CommandSender sender,
return true;
}

final String nameColor = ChatColor.translateAlternateColorCodes(
'&', String.join(" ", args));
final String nameColor = Utility.translateLegacyColors(String.join(" ", args));
final String name = nameColor.substring(0, Math.min(16, nameColor.length()));
final long millis = lastUsedMillis.getOrDefault(player, 0L);
final long millisDifference = System.currentTimeMillis() - millis;
Expand Down Expand Up @@ -61,10 +60,11 @@ public boolean onCommand(final @Nonnull CommandSender sender,
return true;
}

final PlayerProfile profile = player.getPlayerProfile();
// Preserve UUIDs, as changing them breaks clients
final PlayerProfile newProfile = Bukkit.createProfileExact(player.getUniqueId(), name);
newProfile.setProperties(player.getPlayerProfile().getProperties());

profile.setName(name); // FIXME: Marked for removal
player.setPlayerProfile(profile);
player.setPlayerProfile(newProfile);
lastUsedMillis.put(player, System.currentTimeMillis());

player.sendMessage(
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/pw/kaboom/extras/modules/block/BlockCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,13 @@
import org.bukkit.Chunk;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.world.ChunkUnloadEvent;

public final class BlockCheck implements Listener {
@EventHandler
void onBlockPlace(final BlockPlaceEvent event) {
try {
final int maxItemStringLength = 3019;

if (event.getItemInHand().toString().length() > maxItemStringLength) {
event.setCancelled(true);
}

event.getBlockPlaced().getState();
} catch (Exception exception) {
event.setCancelled(true);
}
}
kaboombot marked this conversation as resolved.
Show resolved Hide resolved

@EventHandler
void onChunkUnload(final ChunkUnloadEvent event) {
for (Chunk chunk : event.getChunk().getWorld().getForceLoadedChunks()) {
chunk.setForceLoaded(false);
}
}

@EventHandler
void onSignChange(final SignChangeEvent event) {
try {
event.getLines();
} catch (Exception exception) {
event.setCancelled(true);
}
}
kaboombot marked this conversation as resolved.
Show resolved Hide resolved
}
122 changes: 9 additions & 113 deletions src/main/java/pw/kaboom/extras/modules/block/BlockPhysics.java
Original file line number Diff line number Diff line change
@@ -1,132 +1,27 @@
package pw.kaboom.extras.modules.block;

import java.util.HashSet;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockFadeEvent;
import org.bukkit.event.block.BlockFormEvent;
import org.bukkit.event.block.BlockFromToEvent;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockRedstoneEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;

import com.destroystokyo.paper.event.block.BlockDestroyEvent;
import org.bukkit.scheduler.BukkitScheduler;
import pw.kaboom.extras.Main;

public final class BlockPhysics implements Listener {

private static final double MINIMUM_TPS = 10;
// This class contains code to prevent large areas of non-solid blocks
// from crashing the server
private static double tps = 20;
private static HashSet<BlockFace> blockFaces = new HashSet<BlockFace>();

@EventHandler
void onBlockDestroy(final BlockDestroyEvent event) {
try {
if (!event.getBlock().getType().isSolid()) {
for (BlockFace face : getBlockFaces()) {
if (event.getBlock().getRelative(face).getType()
!= event.getBlock().getType()) {
return;
}
if (!event.getBlock().getType().equals(Material.AIR)) {
event.getBlock().setType(Material.AIR, false);
}
if (!event.isCancelled()) {
event.setCancelled(true);
}
}
}
} catch (Exception | StackOverflowError e) {
event.setCancelled(true);
}
}

@EventHandler
void onBlockFade(final BlockFadeEvent event) {
try {
if (event.getBlock().getType() == Material.FIRE) {
event.getBlock().setType(Material.AIR, false);
event.setCancelled(true);
}
} catch (Exception | StackOverflowError e) {
event.setCancelled(true);
}
}

@EventHandler
void onBlockForm(final BlockFormEvent event) {
try {
if (event.getBlock().getType() == Material.LAVA
|| event.getBlock().getType() == Material.WATER) {
for (BlockFace face : getBlockFaces()) {
if (event.getBlock().getRelative(face).getType() != Material.LAVA
&& event.getBlock().getRelative(face).getType() != Material.WATER) {
return;
}
event.setCancelled(true);
}
}
} catch (Exception | StackOverflowError e) {
event.setCancelled(true);
}
}

@EventHandler
void onBlockFromTo(final BlockFromToEvent event) {
try {
if (event.getBlock().getType() == Material.LAVA
|| event.getBlock().getType() == Material.WATER) {
boolean lavaFound = false;
boolean waterFound = false;

for (BlockFace face : getBlockFaces()) {
if (event.getBlock().getRelative(face).getType() == Material.LAVA
&& !lavaFound) {
lavaFound = true;
} else if (event.getBlock().getRelative(face).getType() == Material.WATER
&& !waterFound) {
waterFound = true;
}

if (lavaFound && waterFound) {
event.setCancelled(true);
return;
}
}
}
} catch (Exception | StackOverflowError e) {
event.setCancelled(true);
}
}

@EventHandler
void onBlockPhysics(final BlockPhysicsEvent event) {
try {
switch (event.getChangedType()) {
case ACTIVATOR_RAIL:
case DETECTOR_RAIL:
case POWERED_RAIL:
case RAIL:
case COMPARATOR:
case REDSTONE_TORCH:
case REDSTONE_WIRE:
case REPEATER:
case TRIPWIRE:
if (!event.getBlock().getRelative(BlockFace.DOWN).getType().isSolid()
&& !Material.AIR.equals(event.getBlock().getRelative(BlockFace.DOWN)
.getType())
&& !Material.CAVE_AIR.equals(event.getBlock()
.getRelative(BlockFace.DOWN).getType())) {
event.setCancelled(true);
}
return;
case COMMAND_BLOCK:
case CHAIN_COMMAND_BLOCK:
case REPEATING_COMMAND_BLOCK:
Expand All @@ -143,15 +38,20 @@ void onBlockPhysics(final BlockPhysicsEvent event) {

@EventHandler
void onBlockRedstone(final BlockRedstoneEvent event) {
final int maxTps = 10;

if (tps < maxTps) {
if (tps < MINIMUM_TPS) {
event.setNewCurrent(0);
}
}

private int fallingBlockCount;

@EventHandler
void onBlockForm(final BlockFormEvent event) {
if (tps < MINIMUM_TPS) {
event.setCancelled(true);
}
}

@EventHandler
void onEntityChangeBlock(final EntityChangeBlockEvent event) {
if (event.getEntityType() == EntityType.FALLING_BLOCK
Expand All @@ -167,10 +67,6 @@ void onEntityChangeBlock(final EntityChangeBlockEvent event) {
}
}

public static HashSet<BlockFace> getBlockFaces() {
return blockFaces;
}

private static void updateTPS() {
final double[] tpsValues = Bukkit.getTPS();

Expand Down
Loading