Cleaned up some code.

This commit is contained in:
Brianna 2021-06-01 13:32:39 -05:00
parent 4e7e6f2585
commit c74c222e16
14 changed files with 99 additions and 110 deletions

View File

@ -22,7 +22,7 @@ import com.songoda.epichoppers.commands.CommandReload;
import com.songoda.epichoppers.commands.CommandSettings;
import com.songoda.epichoppers.database.DataManager;
import com.songoda.epichoppers.database.migrations._1_InitialMigration;
import com.songoda.epichoppers.handlers.TeleportHandler;
import com.songoda.epichoppers.hopper.teleport.TeleportHandler;
import com.songoda.epichoppers.hopper.HopperManager;
import com.songoda.epichoppers.hopper.levels.Level;
import com.songoda.epichoppers.hopper.levels.LevelManager;

View File

@ -9,7 +9,7 @@ import com.songoda.epichoppers.hopper.Hopper;
import com.songoda.epichoppers.hopper.HopperBuilder;
import com.songoda.epichoppers.hopper.ItemType;
import com.songoda.epichoppers.hopper.LinkType;
import com.songoda.epichoppers.utils.TeleportTrigger;
import com.songoda.epichoppers.hopper.teleport.TeleportTrigger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;

View File

@ -14,7 +14,7 @@ import com.songoda.epichoppers.player.SyncType;
import com.songoda.epichoppers.settings.Settings;
import com.songoda.epichoppers.utils.CostType;
import com.songoda.epichoppers.utils.Methods;
import com.songoda.epichoppers.utils.TeleportTrigger;
import com.songoda.epichoppers.hopper.teleport.TeleportTrigger;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;

View File

@ -1,5 +1,7 @@
package com.songoda.epichoppers.hopper;
import com.songoda.core.compatibility.CompatibleParticleHandler;
import com.songoda.core.compatibility.CompatibleSound;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.core.gui.GuiManager;
import com.songoda.core.hooks.EconomyManager;
@ -7,14 +9,14 @@ import com.songoda.epichoppers.EpicHoppers;
import com.songoda.epichoppers.api.events.HopperAccessEvent;
import com.songoda.epichoppers.gui.GUIOverview;
import com.songoda.epichoppers.hopper.levels.Level;
import com.songoda.epichoppers.hopper.teleport.TeleportTrigger;
import com.songoda.epichoppers.player.PlayerData;
import com.songoda.epichoppers.settings.Settings;
import com.songoda.epichoppers.utils.CostType;
import com.songoda.epichoppers.utils.Methods;
import com.songoda.epichoppers.utils.TeleportTrigger;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
@ -123,20 +125,16 @@ public class Hopper {
}
Location loc = location.clone().add(.5, .5, .5);
if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_12)) return;
player.getWorld().spawnParticle(org.bukkit.Particle.valueOf(plugin.getConfig().getString("Main.Upgrade Particle Type")), loc, 200, .5, .5, .5);
CompatibleParticleHandler.spawnParticles(CompatibleParticleHandler.ParticleType.getParticle(Settings.UPGRADE_PARTICLE_TYPE.getString()),
loc, 100, .5, .5, .5);
if (plugin.getLevelManager().getHighestLevel() != level) {
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 0.6F, 15.0F);
player.playSound(player.getLocation(), CompatibleSound.ENTITY_PLAYER_LEVELUP.getSound(), 0.6F, 15.0F);
} else {
player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_LEVELUP, 2F, 25.0F);
if (!ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) return;
player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 2F, 25.0F);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.2F, 35.0F), 5L);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1.8F, 35.0F), 10L);
player.playSound(player.getLocation(), CompatibleSound.ENTITY_PLAYER_LEVELUP.getSound(), 2F, 25.0F);
player.playSound(player.getLocation(), CompatibleSound.BLOCK_NOTE_BLOCK_CHIME.getSound(), 2F, 25.0F);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), CompatibleSound.BLOCK_NOTE_BLOCK_CHIME.getSound(), 1.2F, 35.0F), 5L);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, () -> player.playSound(player.getLocation(), CompatibleSound.BLOCK_NOTE_BLOCK_CHIME.getSound(), 1.8F, 35.0F), 10L);
}
}
@ -155,7 +153,7 @@ public class Hopper {
instance.getLocale().getMessage("event.hopper.synctimeout").sendPrefixedMessage(player);
playerData.setSyncType(null);
}
}, instance.getConfig().getLong("Main.Timeout When Syncing Hoppers") * level.getLinkAmount());
}, Settings.LINK_TIMEOUT.getLong() * level.getLinkAmount());
}
public void link(Block toLink, boolean filtered, Player player) {

View File

@ -1,7 +1,7 @@
package com.songoda.epichoppers.hopper;
import com.songoda.epichoppers.hopper.levels.Level;
import com.songoda.epichoppers.utils.TeleportTrigger;
import com.songoda.epichoppers.hopper.teleport.TeleportTrigger;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;

View File

@ -1,13 +1,13 @@
package com.songoda.epichoppers.handlers;
package com.songoda.epichoppers.hopper.teleport;
import com.songoda.core.compatibility.CompatibleSound;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.epichoppers.EpicHoppers;
import com.songoda.epichoppers.hopper.Hopper;
import com.songoda.epichoppers.settings.Settings;
import com.songoda.epichoppers.utils.Methods;
import com.songoda.epichoppers.utils.TeleportTrigger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
@ -24,43 +24,45 @@ public class TeleportHandler {
private final Map<UUID, Long> lastTeleports = new HashMap<>();
private EpicHoppers plugin;
private final EpicHoppers plugin;
public TeleportHandler(EpicHoppers plugin) {
this.plugin = plugin;
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::teleportRunner, 0, plugin.getConfig().getLong("Main.Amount of Ticks Between Teleport"));
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, this::teleportRunner, 0,
Settings.TELEPORT_TICKS.getLong());
}
private void teleportRunner() {
if (plugin.getHopperManager().isReady()) {
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
if (!(entity instanceof LivingEntity) || entity.getType() == EntityType.ARMOR_STAND)
if (!plugin.getHopperManager().isReady())
return;
for (World world : Bukkit.getWorlds()) {
for (Entity entity : world.getEntities()) {
if (!(entity instanceof LivingEntity) || entity.getType() == EntityType.ARMOR_STAND)
continue;
if (!Settings.TELEPORT.getBoolean()
|| (entity instanceof Player && !entity.hasPermission("EpicHoppers.Teleport")))
continue;
Location location = entity.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation();
if (!this.plugin.getHopperManager().isHopper(location))
continue;
Hopper hopper = this.plugin.getHopperManager().getHopper(location);
if (hopper.getTeleportTrigger() != TeleportTrigger.WALK_ON)
continue;
if (this.lastTeleports.containsKey(entity.getUniqueId())) {
long duration = (new Date()).getTime() - new Date(this.lastTeleports.get(entity.getUniqueId())).getTime();
if (duration <= 5 * 1000)
continue;
if (!this.plugin.getConfig().getBoolean("Main.Allow Players To Teleport Through Hoppers")
|| (entity instanceof Player && !entity.hasPermission("EpicHoppers.Teleport")))
continue;
Location location = entity.getLocation().getBlock().getRelative(BlockFace.DOWN).getLocation();
if (!this.plugin.getHopperManager().isHopper(location))
continue;
Hopper hopper = this.plugin.getHopperManager().getHopper(location);
if (hopper.getTeleportTrigger() != TeleportTrigger.WALK_ON)
continue;
if (this.lastTeleports.containsKey(entity.getUniqueId())) {
long duration = (new Date()).getTime() - new Date(this.lastTeleports.get(entity.getUniqueId())).getTime();
if (duration <= 5 * 1000)
continue;
}
this.tpEntity(entity, hopper);
this.lastTeleports.put(entity.getUniqueId(), System.currentTimeMillis());
}
this.tpEntity(entity, hopper);
this.lastTeleports.put(entity.getUniqueId(), System.currentTimeMillis());
}
}
}
@ -79,7 +81,6 @@ public class TeleportHandler {
*
* @param lastHopper The previous hopper found in the chain
* @param currentChainLength The current length of the chain, used to cap the search length
*
* @return The hopper at the end of the chain (or up to 15 in depth)
*/
private Hopper getChain(Hopper lastHopper, int currentChainLength) {

View File

@ -0,0 +1,7 @@
package com.songoda.epichoppers.hopper.teleport;
public enum TeleportTrigger {
DISABLED, WALK_ON, SNEAK
}

View File

@ -75,7 +75,7 @@ public class BlockListeners implements Listener {
if (num > limit)
limit = num;
}
if (limit == -1) limit = plugin.getConfig().getInt("Main.Max Hoppers Per Chunk");
if (limit == -1) limit = Settings.MAX_CHUNK.getInt();
return limit;
}

View File

@ -25,10 +25,10 @@ import org.bukkit.inventory.ItemStack;
*/
public class HopperListeners implements Listener {
private final EpicHoppers instance;
private final EpicHoppers plugin;
public HopperListeners(EpicHoppers instance) {
this.instance = instance;
public HopperListeners(EpicHoppers plugin) {
this.plugin = plugin;
}
// todo: InventoryMoveItemEvent for filters
@ -40,7 +40,7 @@ public class HopperListeners implements Listener {
Location sourceLocation = source.getHolder() instanceof BlockState ? ((BlockState) source.getHolder()).getLocation() : null;
Location destinationLocation = destination.getHolder() instanceof BlockState ? ((BlockState) destination.getHolder()).getLocation() : null;
if (sourceLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(sourceLocation))
if (sourceLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getHopperManager().isHopper(sourceLocation))
return;
// Hopper minecarts should be able to take care of themselves
@ -66,14 +66,14 @@ public class HopperListeners implements Listener {
// Special cases when a hopper is picking up items
if (destination.getHolder() instanceof org.bukkit.block.Hopper) {
if (destinationLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(destinationLocation))
if (destinationLocation != null && Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getHopperManager().isHopper(destinationLocation))
return;
// Calling HopperManager#getHopper() automatically creates a new Hopper and we don't need to iterate over default-valued hoppers
if (!instance.getHopperManager().isHopper(destinationLocation))
if (!plugin.getHopperManager().isHopper(destinationLocation))
return;
Hopper toHopper = instance.getHopperManager().getHopper(destinationLocation);
Hopper toHopper = plugin.getHopperManager().getHopper(destinationLocation);
// minecraft 1.8 doesn't have a method to get the hopper's location from the inventory, so we use the holder instead
final ItemStack toMove = event.getItem();

View File

@ -7,7 +7,7 @@ import com.songoda.epichoppers.hopper.Hopper;
import com.songoda.epichoppers.player.PlayerData;
import com.songoda.epichoppers.player.SyncType;
import com.songoda.epichoppers.settings.Settings;
import com.songoda.epichoppers.utils.TeleportTrigger;
import com.songoda.epichoppers.hopper.teleport.TeleportTrigger;
import com.songoda.skyblock.SkyBlock;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -27,26 +27,26 @@ import org.bukkit.inventory.InventoryHolder;
*/
public class InteractListeners implements Listener {
private final EpicHoppers instance;
private final EpicHoppers plugin;
public InteractListeners(EpicHoppers instance) {
this.instance = instance;
public InteractListeners(EpicHoppers plugin) {
this.plugin = plugin;
}
@EventHandler
public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent event) {
Player player = event.getPlayer();
if (player.isSneaking() && instance.getHopperManager().isReady()) {
if (player.isSneaking() && plugin.getHopperManager().isReady()) {
Location location = player.getLocation().getBlock().getRelative(BlockFace.SELF).getLocation();
Location down = location.getBlock().getRelative(BlockFace.DOWN).getLocation();
if (instance.getHopperManager().isHopper(down)) {
Hopper hopper = instance.getHopperManager().getHopper(down);
if (plugin.getHopperManager().isHopper(down)) {
Hopper hopper = plugin.getHopperManager().getHopper(down);
if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK)
instance.getTeleportHandler().tpEntity(player, hopper);
} else if (instance.getHopperManager().isHopper(location)) {
Hopper hopper = instance.getHopperManager().getHopper(location);
plugin.getTeleportHandler().tpEntity(player, hopper);
} else if (plugin.getHopperManager().isHopper(location)) {
Hopper hopper = plugin.getHopperManager().getHopper(location);
if (hopper.getTeleportTrigger() == TeleportTrigger.SNEAK)
instance.getTeleportHandler().tpEntity(player, hopper);
plugin.getTeleportHandler().tpEntity(player, hopper);
}
}
}
@ -66,7 +66,7 @@ public class InteractListeners implements Listener {
return;
if (Settings.USE_PROTECTION_PLUGINS.getBoolean() && !ProtectionManager.canInteract(player, event.getClickedBlock().getLocation())) {
player.sendMessage(instance.getLocale().getMessage("event.general.protected").getPrefixedMessage());
player.sendMessage(plugin.getLocale().getMessage("event.general.protected").getPrefixedMessage());
return;
}
@ -80,22 +80,22 @@ public class InteractListeners implements Listener {
return;
}
PlayerData playerData = instance.getPlayerDataManager().getPlayerData(player);
PlayerData playerData = plugin.getPlayerDataManager().getPlayerData(player);
if (playerData.getSyncType() == null) {
if (event.getClickedBlock().getType() == Material.HOPPER) {
if (!instance.getHopperManager().isReady()) {
player.sendMessage(instance.getLocale().getMessage("event.hopper.notready").getMessage());
if (!plugin.getHopperManager().isReady()) {
player.sendMessage(plugin.getLocale().getMessage("event.hopper.notready").getMessage());
event.setCancelled(true);
return;
}
if (Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !instance.getHopperManager().isHopper(event.getClickedBlock().getLocation()))
if (Settings.ALLOW_NORMAL_HOPPERS.getBoolean() && !plugin.getHopperManager().isHopper(event.getClickedBlock().getLocation()))
return;
Hopper hopper = instance.getHopperManager().getHopper(event.getClickedBlock());
Hopper hopper = plugin.getHopperManager().getHopper(event.getClickedBlock());
if (!player.getInventory().getItemInHand().getType().name().contains("PICKAXE")) {
hopper.overview(instance.getGuiManager(), player);
hopper.overview(plugin.getGuiManager(), player);
event.setCancelled(true);
return;
}
@ -103,13 +103,15 @@ public class InteractListeners implements Listener {
return;
}
if (event.getClickedBlock().getState() instanceof InventoryHolder || (event.getClickedBlock().getType().equals(Material.ENDER_CHEST) && instance.getConfig().getBoolean("Main.Support Enderchests"))) {
if (event.getClickedBlock().getState() instanceof InventoryHolder
|| (event.getClickedBlock().getType().equals(Material.ENDER_CHEST)
&& Settings.ENDERCHESTS.getBoolean())) {
Hopper hopper = playerData.getLastHopper();
if (event.getClickedBlock().getLocation().equals(playerData.getLastHopper().getLocation())) {
if (!hopper.getLinkedBlocks().isEmpty())
instance.getLocale().getMessage("event.hopper.syncfinish").sendPrefixedMessage(player);
plugin.getLocale().getMessage("event.hopper.syncfinish").sendPrefixedMessage(player);
else
instance.getLocale().getMessage("event.hopper.synccanceled").sendPrefixedMessage(player);
plugin.getLocale().getMessage("event.hopper.synccanceled").sendPrefixedMessage(player);
hopper.cancelSync(player);
} else if (playerData.getSyncType() != null) {
hopper.link(event.getClickedBlock(), playerData.getSyncType() == SyncType.FILTERED, player);

View File

@ -36,7 +36,7 @@ public class Settings {
public static final ConfigSetting ENDERCHESTS = new ConfigSetting(config, "Main.Support Enderchests", true,
"Should hoppers dump items into a player enderchests?");
public static final ConfigSetting PARTICLE_TYPE = new ConfigSetting(config, "Main.Upgrade Particle Type", "SPELL_WITCH",
public static final ConfigSetting UPGRADE_PARTICLE_TYPE = new ConfigSetting(config, "Main.Upgrade Particle Type", "SPELL_WITCH",
"The type of particle shown when a hopper is upgraded.");
public static final ConfigSetting HOP_TICKS = new ConfigSetting(config, "Main.Amount of Ticks Between Hops", 8L,
@ -52,7 +52,7 @@ public class Settings {
"The cooldown between teleports. It prevents players",
"from getting stuck in a teleport loop.");
public static final ConfigSetting SYNC_TIMEOUT = new ConfigSetting(config, "Main.Timeout When Syncing Hoppers", 300L,
public static final ConfigSetting LINK_TIMEOUT = new ConfigSetting(config, "Main.Timeout When Syncing Hoppers", 300L,
"The amount of time in ticks a player has between hitting the hopper",
"Link button and performing the link. When the time is up the link event is canceled.");

View File

@ -1,18 +1,7 @@
package com.songoda.epichoppers.utils;
/**
* Represents a cost type when making a purchase from EpicSpawners
*/
public enum CostType {
/**
* A purchase made with an economy balance (generally an implementation of Vault)
*/
ECONOMY,
/**
* A purchase made with a player's experience levels
*/
EXPERIENCE
ECONOMY, EXPERIENCE
}

View File

@ -11,12 +11,12 @@ public enum HopperDirection {
WEST(4, 12, -1, 0, 0),
EAST(5, 13, 1, 0, 0);
private int unpowered;
private int powered;
private final int unpowered;
private final int powered;
private int x;
private int y;
private int z;
private final int x;
private final int y;
private final int z;
HopperDirection(int unpowered, int powered, int x, int y, int z) {
this.unpowered = unpowered;
@ -28,10 +28,9 @@ public enum HopperDirection {
}
public static HopperDirection getDirection(int value) {
for (HopperDirection hopperDirection : HopperDirection.values()) {
if (hopperDirection.getPowered() == value
|| hopperDirection.getUnpowered() == value) return hopperDirection;
}
for (HopperDirection hopperDirection : HopperDirection.values())
if (hopperDirection.getPowered() == value || hopperDirection.getUnpowered() == value)
return hopperDirection;
return null;
}

View File

@ -1,7 +0,0 @@
package com.songoda.epichoppers.utils;
public enum TeleportTrigger {
DISABLED,
WALK_ON,
SNEAK
}