RandomTeleport: Factions and cooldown

This commit is contained in:
Erik Bročko 2018-09-09 19:14:58 +02:00
parent 1653ceb5fe
commit c588c53a9e
2 changed files with 82 additions and 18 deletions

View File

@ -23,8 +23,8 @@ public class MarwCore extends JavaPlugin {
//getServer().getPluginManager().registerEvents(new EventListener(this), this);
configManager = new ConfigManager(this);
this.addFeature(new Base());
this.addFeature(new Base());
this.addFeature(new Borders());
this.addFeature(new ChatNotifier());
this.addFeature(new RandomTeleport());

View File

@ -1,9 +1,13 @@
package cz.marwland.mc.essentials.features;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
@ -13,11 +17,29 @@ import org.bukkit.event.block.BlockCanBuildEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import cz.marwland.mc.core.features.Feature;
import cz.marwland.mc.core.util.UserUtil;
import net.md_5.bungee.api.ChatColor;
public class RandomTeleport extends Feature {
private int MAX_TRIES = 100;
private final int MAX_TRIES = 100;
private final long DEFAULT_COOLDOWN = 15000;
private HashMap<Player, Long> cooldownMap = new HashMap<>();
private static final Set<Material> INVALID_MATERIALS = new HashSet<>(Arrays.asList(
Material.AIR,
Material.WATER,
Material.STATIONARY_WATER,
Material.LAVA,
Material.STATIONARY_LAVA,
Material.MAGMA,
Material.MAGMA_CREAM,
Material.FIRE,
Material.WEB,
Material.CACTUS,
Material.ENDER_PORTAL,
Material.PORTAL
));
public RandomTeleport() {
super();
@ -38,7 +60,6 @@ public class RandomTeleport extends Feature {
Player p = (Player) sender;
if (args.length >= 1) {
p = Bukkit.getServer().getPlayer(args[0]);
if (p != sender && !permissionMissingCheck(sender, this.getPermission() + ".others")) {
return true;
@ -51,14 +72,24 @@ public class RandomTeleport extends Feature {
World w = p.getWorld();
if (args.length >= 2) {
w = Bukkit.getServer().getWorld(args[1]);
if (w != p.getWorld() && !permissionMissingCheck(sender, this.getPermission() + ".world." + w.getName()) ) {
return true;
} else if (w == null) {
if (w == null) {
sender.sendMessage(ChatColor.RED + "World not found!");
return true;
}
}
if (!sender.hasPermission(this.getPermission() + ".world." + w.getName())) {
sender.sendMessage(ChatColor.RED + "V tomhle svete se nemuzes nahodne teleportovat!");
return true;
}
long left = (long) Math.ceil((double) cooldownLeft(p) / 1000d);
if (left > 0) {
sender.sendMessage(ChatColor.RED + "Nemuzes se nahodne teleportovat jeste " + ChatColor.YELLOW + left + ChatColor.RED + " sekund" + (left == 1 ? "u!" : (left > 4 ? "!" : "y!")));
return true;
}
cooldownMap.put(p, System.currentTimeMillis());
randomTeleport(w, p);
return true;
@ -74,13 +105,21 @@ public class RandomTeleport extends Feature {
public void randomTeleport(World w, Player p) {
Location loc = null;
for (int i = 0; i < MAX_TRIES && loc == null; i++) {
p.sendMessage("Generating #" + i);
loc = generateLoc(w);
if (!canTeleportHere(loc, p))
if (!canTeleportHere(loc, p, false))
loc = null;
else {
loc.add(0.5d, 1d, 0.5d);
if (!canTeleportHere(loc, p, true))
loc = null;
}
}
if (loc != null)
if (loc != null) {
loc.setYaw((float) (Math.random() * 360f));
p.teleport(loc, TeleportCause.COMMAND);
} else {
p.sendMessage(ChatColor.RED + "V tomhle svete se pro tebe nenaslo bezpecne misto!");
}
}
public Location generateLoc(World w) {
@ -98,31 +137,56 @@ public class RandomTeleport extends Feature {
int finx = minx + (int) (Math.random() * rangex) + wboff;
int finz = minz + (int) (Math.random() * rangez) + wboff;
Location targetloc = w.getHighestBlockAt(finx, finz).getLocation();
targetloc.setYaw((float) (Math.random() * 360f));
targetloc.add(0.5d, 1d, 0.5d);
targetloc.subtract(0d, 1d, 0d);
return targetloc;
}
public boolean canTeleportHere(Location loc, Player p) {
public boolean canTeleportHere(Location loc, Player p, boolean allowAir) {
Block block = loc.getWorld().getBlockAt(loc);
p.sendMessage(block.getType().name());
if (INVALID_MATERIALS.contains(block.getType()) && (allowAir ? block.getType() != Material.AIR : true))
return false;
BlockCanBuildEvent canBuildEvent = new BlockCanBuildEvent(block, block.getData(), true);
Bukkit.getServer().getPluginManager().callEvent(canBuildEvent);
if(!canBuildEvent.isBuildable())
if (!canBuildEvent.isBuildable())
return false;
try {
p.sendMessage("trying");
if (!com.sk89q.worldguard.bukkit.WGBukkit.getPlugin().canBuild(p, block)) {
p.sendMessage("cannot");
return false;
}
} catch (NoClassDefFoundError e) {
}
p.sendMessage("can");
try {
if (com.massivecraft.factions.entity.BoardColl.get().getFactionAt(com.massivecraft.massivecore.ps.PS.valueOf(loc)) != com.massivecraft.factions.entity.FactionColl.get().getNone()) {
return false;
}
} catch (NoClassDefFoundError e) {
}
return true;
}
public long cooldownLeft(Player p) {
long cooldown = DEFAULT_COOLDOWN;
// marw.core.randomteleport.cooldown
String meta = UserUtil.getPlayersMeta(p, this.getPermissionPath() + ".cooldown", "15");
if (meta != null) {
try {
cooldown = Long.parseLong(meta) * 1000l;
} catch (NumberFormatException ex) {
plugin.getLogger().info("ERROR: " + this.getPermissionPath() + ".cooldown" + " is not a long for " + p.getName() + ": " + meta);
}
}
Long lastExecuted = cooldownMap.get(p);
if (lastExecuted == null)
return 0l;
long left = cooldown - (System.currentTimeMillis() - lastExecuted.longValue());
return left;
}
}