Minor code style changes (mostly use of this. for instance fields)

This commit is contained in:
Christian Koop 2023-05-18 12:07:47 +02:00
parent cc2307f532
commit 47fd57a7ac
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
17 changed files with 70 additions and 124 deletions

View File

@ -58,13 +58,13 @@ public class AnchorManager {
}
protected void saveAll() {
for (Set<Anchor> anchorSet : anchors.values()) {
for (Set<Anchor> anchorSet : this.anchors.values()) {
this.dataManager.updateAnchors(anchorSet, null);
}
}
protected void deInitAll() {
for (World world : anchors.keySet().toArray(new World[0])) {
for (World world : this.anchors.keySet().toArray(new World[0])) {
deInitAnchors(world);
}
}
@ -123,18 +123,18 @@ public class AnchorManager {
protected void setReady() {
this.ready = true;
Bukkit.getScheduler().runTaskTimer(plugin, this::saveAll, 20L * 60 * 5, 20L * 60 * 5);
Bukkit.getScheduler().runTaskTimer(this.plugin, this::saveAll, 20L * 60 * 5, 20L * 60 * 5);
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isReady(World world) {
return this.ready && anchors.containsKey(world);
return this.ready && this.anchors.containsKey(world);
}
/* Getter */
public Anchor[] getAnchors(@NotNull World world) {
Set<Anchor> set = anchors.get(world);
Set<Anchor> set = this.anchors.get(world);
if (set != null) {
return set.toArray(new Anchor[0]);
@ -149,7 +149,7 @@ public class AnchorManager {
}
Location bLoc = b.getLocation();
Set<Anchor> set = anchors.get(b.getWorld());
Set<Anchor> set = this.anchors.get(b.getWorld());
if (set != null) {
for (Anchor anchor : set) {
@ -171,7 +171,7 @@ public class AnchorManager {
throw new IllegalStateException(ERR_WORLD_NOT_READY);
}
Set<Anchor> set = anchors.get(chunk.getWorld());
Set<Anchor> set = this.anchors.get(chunk.getWorld());
if (set != null) {
for (Anchor anchor : set) {
@ -237,7 +237,7 @@ public class AnchorManager {
Block b = loc.getBlock();
b.setType(Settings.MATERIAL.getMaterial().getMaterial());
anchors.computeIfAbsent(anchor.getWorld(), k -> new HashSet<>())
this.anchors.computeIfAbsent(anchor.getWorld(), k -> new HashSet<>())
.add(anchor);
updateHologram(anchor);
@ -259,7 +259,7 @@ public class AnchorManager {
throw new IllegalStateException(ERR_WORLD_NOT_READY);
}
for (Set<Anchor> value : anchors.values()) {
for (Set<Anchor> value : this.anchors.values()) {
value.remove(anchor);
}
@ -293,9 +293,9 @@ public class AnchorManager {
@SuppressWarnings("unused")
public void registerAccessCheck(AnchorAccessCheck accessCheck) {
if (!accessChecks.contains(accessCheck)) {
if (!this.accessChecks.contains(accessCheck)) {
// Adding at the start of the list makes sure the default check is
accessChecks.add(accessCheck);
this.accessChecks.add(accessCheck);
}
}
@ -306,7 +306,7 @@ public class AnchorManager {
*/
@SuppressWarnings("unused")
public boolean unregisterAccessCheck(AnchorAccessCheck accessCheck) {
return accessChecks.remove(accessCheck);
return this.accessChecks.remove(accessCheck);
}
public boolean hasAccess(@NotNull Anchor anchor, @NotNull OfflinePlayer p) {
@ -314,7 +314,7 @@ public class AnchorManager {
}
/**
* Checks if a player has access to an Anchor. By default only the owner has access to an Anchor.
* Checks if a player has access to an Anchor. By default, only the owner has access to an Anchor.
* <br>
* Other plugins can grant access to other players (e.g. friends).
* <br>

View File

@ -45,15 +45,13 @@ public final class EpicAnchors extends SongodaPlugin {
@Override
public void onPluginEnable() {
// Songoda Updater
SongodaCore.registerPlugin(this, 31, CompatibleMaterial.END_PORTAL_FRAME);
// Initialize database
this.getLogger().info("Initializing SQLite...");
DatabaseConnector dbCon = new SQLiteConnector(this);
this.dataManager = new DataManager(dbCon, this);
AnchorMigration anchorMigration = new AnchorMigration(dbCon, this.dataManager,
new _1_InitialMigration());
AnchorMigration anchorMigration = new AnchorMigration(dbCon, this.dataManager, new _1_InitialMigration());
anchorMigration.runMigrations();
anchorMigration.migrateLegacyData(this);
@ -74,7 +72,7 @@ public final class EpicAnchors extends SongodaPlugin {
// Event Listener
this.guiManager = new GuiManager(this);
guiManager.init();
this.guiManager.init();
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new WorldListener(
world -> this.anchorManager.initAnchorsAsync(world, null),

View File

@ -32,7 +32,7 @@ public class GiveCommand extends AbstractCommand {
if (target == null &&
!args[0].trim().equalsIgnoreCase("all") &&
!args[0].trim().equalsIgnoreCase("@a")) {
plugin.getLocale().newMessage("&cThat is not a player...").sendPrefixedMessage(sender);
this.plugin.getLocale().newMessage("&cThat is not a player...").sendPrefixedMessage(sender);
return ReturnType.SYNTAX_ERROR;
}
@ -40,22 +40,22 @@ public class GiveCommand extends AbstractCommand {
ItemStack itemStack;
if (Utils.isInt(args[1]) && Integer.parseInt(args[1]) > 0) {
itemStack = plugin.getAnchorManager().createAnchorItem(Integer.parseInt(args[1]) * 20 * 60 * 60);
itemStack = this.plugin.getAnchorManager().createAnchorItem(Integer.parseInt(args[1]) * 20 * 60 * 60);
} else if (args[1].equalsIgnoreCase("infinite")) {
itemStack = plugin.getAnchorManager().createAnchorItem(-1);
itemStack = this.plugin.getAnchorManager().createAnchorItem(-1);
} else {
plugin.getLocale().newMessage("&cYou can only use positive whole numbers...").sendPrefixedMessage(sender);
this.plugin.getLocale().newMessage("&cYou can only use positive whole numbers...").sendPrefixedMessage(sender);
return ReturnType.FAILURE;
}
if (target != null) {
target.getInventory().addItem(itemStack);
plugin.getLocale().getMessage("command.give.success").sendPrefixedMessage(target);
this.plugin.getLocale().getMessage("command.give.success").sendPrefixedMessage(target);
} else {
for (Player online : Bukkit.getOnlinePlayers()) {
online.getInventory().addItem(itemStack);
plugin.getLocale().getMessage("command.give.success").sendPrefixedMessage(online);
this.plugin.getLocale().getMessage("command.give.success").sendPrefixedMessage(online);
}
}

View File

@ -17,8 +17,8 @@ public class ReloadCommand extends AbstractCommand {
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
plugin.reloadConfig();
plugin.getLocale().getMessage("&7Configuration and Language files reloaded.").sendPrefixedMessage(sender);
this.plugin.reloadConfig();
this.plugin.getLocale().getMessage("&7Configuration and Language files reloaded.").sendPrefixedMessage(sender);
return ReturnType.SUCCESS;
}

View File

@ -23,7 +23,7 @@ public class SettingsCommand extends AbstractCommand {
@Override
protected ReturnType runCommand(CommandSender sender, String... args) {
guiManager.showGUI((Player) sender, new PluginConfigGui(instance));
this.guiManager.showGUI((Player) sender, new PluginConfigGui(this.instance));
return AbstractCommand.ReturnType.SUCCESS;
}

View File

@ -30,7 +30,7 @@ public class ShowCommand extends AbstractCommand {
boolean visualize = this.plugin.getAnchorManager().toggleChunkVisualized((Player) sender);
plugin.getLocale().getMessage("command.show." + (visualize ? "start" : "stop"))
this.plugin.getLocale().getMessage("command.show." + (visualize ? "start" : "stop"))
.sendPrefixedMessage(sender);
return ReturnType.SUCCESS;

View File

@ -159,7 +159,7 @@ public class DataManager extends DataManagerAbstract {
for (int i : batchRes) {
if (i < 0 && i != Statement.SUCCESS_NO_INFO) {
throw new AssertionError("Batch-INSERT failed for at least one statement with code " + i + "");
throw new AssertionError("Batch-INSERT failed for at least one statement with code " + i);
}
}
} catch (SQLException ex) {

View File

@ -67,7 +67,7 @@ public class AnchorMigration extends DataMigrationManager {
int z = Location.locToBlock(Double.parseDouble(locArgs[3]));
int finalTicksLeft = ticksLeft;
dataManager.exists(worldName, x, y, z, (ex, anchorExists) -> {
this.dataManager.exists(worldName, x, y, z, (ex, anchorExists) -> {
if (ex == null) {
if (anchorExists) {
cfgSection.set(locationStr, null);

View File

@ -36,26 +36,26 @@ public class AnchorGui extends Gui {
prepareGui(this.plugin, this, this.anchor);
if (Settings.ADD_TIME_WITH_XP.getBoolean()) {
String itemName = plugin.getLocale().getMessage("interface.button.addtimewithxp").getMessage();
String itemLore = plugin.getLocale().getMessage("interface.button.addtimewithxplore")
String itemName = this.plugin.getLocale().getMessage("interface.button.addtimewithxp").getMessage();
String itemLore = this.plugin.getLocale().getMessage("interface.button.addtimewithxplore")
.processPlaceholder("cost", Settings.XP_COST.getInt())
.getMessage();
setButton(11,
GuiUtils.createButtonItem(Settings.XP_ICON.getMaterial(CompatibleMaterial.EXPERIENCE_BOTTLE), itemName, itemLore),
event -> buyTime(anchor, event.player, false));
event -> buyTime(this.anchor, event.player, false));
}
if (EconomyManager.isEnabled() && Settings.ADD_TIME_WITH_ECONOMY.getBoolean()) {
String itemName = plugin.getLocale().getMessage("interface.button.addtimewitheconomy").getMessage();
String itemLore = plugin.getLocale().getMessage("interface.button.addtimewitheconomylore")
String itemName = this.plugin.getLocale().getMessage("interface.button.addtimewitheconomy").getMessage();
String itemLore = this.plugin.getLocale().getMessage("interface.button.addtimewitheconomylore")
// EconomyManager#formatEconomy adds its own prefix/suffix
.processPlaceholder("cost", EconomyManager.formatEconomy(Settings.ECONOMY_COST.getDouble()))
.getMessage();
setButton(15,
GuiUtils.createButtonItem(Settings.ECO_ICON.getMaterial(CompatibleMaterial.SUNFLOWER), itemName, itemLore),
event -> buyTime(anchor, event.player, true));
event -> buyTime(this.anchor, event.player, true));
}
}

View File

@ -46,18 +46,18 @@ public class DestroyConfirmationGui extends Gui {
private void constructGUI() {
AnchorGui.prepareGui(this.plugin, this, this.anchor);
String cancelLore = plugin.getLocale().getMessage("interface.button.cancelDestroyLore").getMessage();
String confirmLore = plugin.getLocale().getMessage("interface.button." +
String cancelLore = this.plugin.getLocale().getMessage("interface.button.cancelDestroyLore").getMessage();
String confirmLore = this.plugin.getLocale().getMessage("interface.button." +
(Settings.ALLOW_ANCHOR_BREAKING.getBoolean() ? "confirmDestroyLore" : "confirmDestroyLoreNoDrops"))
.getMessage();
setButton(11, GuiUtils.createButtonItem(CompatibleMaterial.RED_TERRACOTTA,
plugin.getLocale().getMessage("interface.button.cancelDestroy").getMessage(),
this.plugin.getLocale().getMessage("interface.button.cancelDestroy").getMessage(),
cancelLore.isEmpty() ? new String[0] : new String[] {cancelLore}),
event -> this.handler.accept(null, false));
setButton(15, GuiUtils.createButtonItem(CompatibleMaterial.GREEN_TERRACOTTA,
plugin.getLocale().getMessage("interface.button.confirmDestroy").getMessage(),
this.plugin.getLocale().getMessage("interface.button.confirmDestroy").getMessage(),
confirmLore.isEmpty() ? new String[0] : new String[] {confirmLore}),
event -> this.handler.accept(null, true));
}

View File

@ -37,7 +37,7 @@ public class AnchorListener implements Listener {
if (item.hasItemMeta() &&
item.getItemMeta().hasDisplayName() &&
Settings.MATERIAL.getMaterial().getMaterial() == e.getBlock().getType()) {
if (!plugin.getAnchorManager().isReady(e.getBlock().getWorld())) {
if (!this.plugin.getAnchorManager().isReady(e.getBlock().getWorld())) {
e.setCancelled(true);
e.getPlayer().sendMessage("Anchors are still being initialized - Please wait a moment"); // TODO
} else {
@ -46,7 +46,7 @@ public class AnchorListener implements Listener {
if (ticksLeft != 0) {
boolean dropOnErr = e.getPlayer().getGameMode() != GameMode.CREATIVE;
plugin.getAnchorManager().createAnchor(e.getBlock().getLocation(), e.getPlayer().getUniqueId(), ticksLeft,
this.plugin.getAnchorManager().createAnchor(e.getBlock().getLocation(), e.getPlayer().getUniqueId(), ticksLeft,
(ex, result) -> {
if (ex != null) {
Utils.logException(this.plugin, ex, "SQLite");
@ -59,7 +59,7 @@ public class AnchorListener implements Listener {
if (dropOnErr) {
e.getBlock().getWorld().dropItemNaturally(e.getBlock().getLocation(),
plugin.getAnchorManager().createAnchorItem(ticksLeft, item.getType()));
this.plugin.getAnchorManager().createAnchorItem(ticksLeft, item.getType()));
}
});
}
@ -72,10 +72,10 @@ public class AnchorListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockInteract(PlayerInteractEvent e) {
if (e.getClickedBlock() == null ||
!plugin.getAnchorManager().isReady(e.getClickedBlock().getWorld())) return;
!this.plugin.getAnchorManager().isReady(e.getClickedBlock().getWorld())) return;
Player p = e.getPlayer();
Anchor anchor = plugin.getAnchorManager().getAnchor(e.getClickedBlock());
Anchor anchor = this.plugin.getAnchorManager().getAnchor(e.getClickedBlock());
if (anchor != null) {
e.setCancelled(true);
@ -90,7 +90,7 @@ public class AnchorListener implements Listener {
Bukkit.getPluginManager().callEvent(blockBreakEvent);
if (!blockBreakEvent.isCancelled()) {
plugin.getAnchorManager().destroyAnchor(anchor);
this.plugin.getAnchorManager().destroyAnchor(anchor);
}
}
}));
@ -115,11 +115,11 @@ public class AnchorListener implements Listener {
anchor.getLocation().add(.5, .5, .5), 100, .5, .5, .5);
}
} else {
plugin.getGuiManager().showGUI(p, new AnchorGui(plugin, anchor));
this.plugin.getGuiManager().showGUI(p, new AnchorGui(this.plugin, anchor));
}
}
} else {
plugin.getLocale().getMessage("event.general.nopermission").sendMessage(p);
this.plugin.getLocale().getMessage("event.general.nopermission").sendMessage(p);
}
}
}

View File

@ -22,7 +22,7 @@ public class BlockListener implements Listener {
private void onBlockBurn(BlockBurnEvent e) {
if (!this.manager.isReady(e.getBlock().getWorld())) return;
if (manager.isAnchor(e.getBlock())) {
if (this.manager.isAnchor(e.getBlock())) {
e.setCancelled(true);
}
}
@ -31,7 +31,7 @@ public class BlockListener implements Listener {
private void onBlockPiston(BlockPistonExtendEvent e) {
if (!this.manager.isReady(e.getBlock().getWorld())) return;
if (manager.isAnchor(e.getBlock())) {
if (this.manager.isAnchor(e.getBlock())) {
e.setCancelled(true);
}
}
@ -40,7 +40,7 @@ public class BlockListener implements Listener {
private void onBlockPiston(BlockPistonRetractEvent e) {
if (!this.manager.isReady(e.getBlock().getWorld())) return;
if (manager.isAnchor(e.getBlock())) {
if (this.manager.isAnchor(e.getBlock())) {
e.setCancelled(true);
}
}
@ -49,7 +49,7 @@ public class BlockListener implements Listener {
private void onBlockPhysics(BlockPhysicsEvent e) {
if (!this.manager.isReady(e.getBlock().getWorld())) return;
if (manager.isAnchor(e.getBlock())) {
if (this.manager.isAnchor(e.getBlock())) {
e.setCancelled(true);
}
}
@ -58,7 +58,7 @@ public class BlockListener implements Listener {
private void onBlockPiston(LeavesDecayEvent e) {
if (!this.manager.isReady(e.getBlock().getWorld())) return;
if (manager.isAnchor(e.getBlock())) {
if (this.manager.isAnchor(e.getBlock())) {
e.setCancelled(true);
}
}
@ -68,7 +68,7 @@ public class BlockListener implements Listener {
if (!this.manager.isReady(e.getWorld())) return;
for (Block b : e.getBlocks()) {
if (manager.isAnchor(b)) {
if (this.manager.isAnchor(b)) {
e.setCancelled(true);
break;
}

View File

@ -20,11 +20,11 @@ public class WorldListener implements Listener {
@EventHandler(priority = EventPriority.MONITOR)
private void onWorldLoad(WorldLoadEvent e) {
initAnchorsInWorld.accept(e.getWorld());
this.initAnchorsInWorld.accept(e.getWorld());
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
private void onWorldUnload(WorldUnloadEvent e) {
deInitAnchorsInWorld.accept(e.getWorld());
this.deInitAnchorsInWorld.accept(e.getWorld());
}
}

View File

@ -69,7 +69,7 @@ public class AnchorTask extends BukkitRunnable {
WorldUtils.loadAnchoredChunk(chunk, this.plugin);
}
if (!randomTicksFailed) {
if (!this.randomTicksFailed) {
try {
NmsManager.getWorld().randomTickChunk(chunk, randomTicks);
} catch (ReflectiveOperationException ex) {
@ -77,11 +77,11 @@ public class AnchorTask extends BukkitRunnable {
() -> "Failed to do random ticks on this server implementation(/version) - " +
"Skipping further random ticks.");
randomTicksFailed = true;
this.randomTicksFailed = true;
}
}
if (!spawnerTicksFailed) {
if (!this.spawnerTicksFailed) {
try {
NmsManager.getWorld().tickInactiveSpawners(chunk, TASK_INTERVAL);
} catch (ReflectiveOperationException ex) {
@ -89,7 +89,7 @@ public class AnchorTask extends BukkitRunnable {
() -> "Failed to do spawner ticks on this server implementation(/version) - " +
"Skipping further spawner ticks.");
spawnerTicksFailed = true;
this.spawnerTicksFailed = true;
}
}
}
@ -113,7 +113,7 @@ public class AnchorTask extends BukkitRunnable {
}
// Update holograms on queued anchors
anchorManager.updateHolograms(toUpdateHolo);
this.anchorManager.updateHolograms(toUpdateHolo);
}
} catch (Exception ex) {
Utils.logException(this.plugin, ex);

View File

@ -30,7 +30,7 @@ public class VisualizeTask extends BukkitRunnable {
}
public void startTask() {
runTaskTimer(plugin, TASK_INTERVAL, TASK_INTERVAL);
runTaskTimer(this.plugin, TASK_INTERVAL, TASK_INTERVAL);
}
@Override
@ -55,10 +55,10 @@ public class VisualizeTask extends BukkitRunnable {
Location pLoc = p.getLocation();
// start and stop chunk coordinates
int cxi = (pLoc.getBlockX() >> 4) - radius;
int cxn = cxi + radius * 2;
int czi = (pLoc.getBlockZ() >> 4) - radius;
int czn = czi + radius * 2;
int cxi = (pLoc.getBlockX() >> 4) - this.radius;
int cxn = cxi + this.radius * 2;
int czi = (pLoc.getBlockZ() >> 4) - this.radius;
int czn = czi + this.radius * 2;
// loop through the chunks to find applicable ones
for (int cx = cxi; cx < cxn; ++cx) {

View File

@ -1,52 +0,0 @@
package com.songoda.epicanchors.utils;
import java.lang.reflect.Field;
public class ReflectionUtils {
private ReflectionUtils() {
throw new IllegalStateException("Utility class");
}
public static Object getFieldValue(Object instance, String fieldName) throws NoSuchFieldException, IllegalAccessException {
Field f = getField(instance, fieldName);
boolean accessible = f.isAccessible();
f.setAccessible(true);
Object result = f.get(instance);
f.setAccessible(accessible);
return result;
}
public static void setFieldValue(Object instance, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field f = getField(instance, fieldName);
boolean accessible = f.isAccessible();
f.setAccessible(true);
f.set(instance, value);
f.setAccessible(accessible);
}
private static Field getField(Object instance, String fieldName) throws NoSuchFieldException {
Field f = null;
Class<?> currClass = instance.getClass();
do {
try {
f = currClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException ex) {
currClass = currClass.getSuperclass();
if (currClass == null) {
throw ex;
}
}
} while (f == null);
return f;
}
}

View File

@ -7,10 +7,10 @@ public class ThreadSync {
private final AtomicReference<Boolean> waiting = new AtomicReference<>(true);
public void waitForRelease() {
synchronized (syncObj) {
while (waiting.get()) {
synchronized (this.syncObj) {
while (this.waiting.get()) {
try {
syncObj.wait();
this.syncObj.wait();
} catch (Exception ignore) {
}
}
@ -18,13 +18,13 @@ public class ThreadSync {
}
public void release() {
synchronized (syncObj) {
waiting.set(false);
syncObj.notifyAll();
synchronized (this.syncObj) {
this.waiting.set(false);
this.syncObj.notifyAll();
}
}
public void reset() {
waiting.set(true);
this.waiting.set(true);
}
}