Moved features to separate projects

This commit is contained in:
2018-09-24 12:51:02 +02:00
commit 8383a77730
35 changed files with 1009 additions and 0 deletions

View File

@ -0,0 +1,169 @@
package cz.marwland.mc.features;
import java.util.Arrays;
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;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.entity.Player;
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.Cooldown;
import net.md_5.bungee.api.ChatColor;
public class RandomTeleport extends Feature {
private final int MAX_TRIES = 100;
private Cooldown telCooldown = new Cooldown(this);
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();
this.addCommand(new BukkitCommand(
"rtp",
"Teleport player to random location in the current world.",
"/rtp [player]",
Arrays.asList("randomtp", "randomteleport") ) {
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!permissionMissingCheck(sender, this.getPermission()))
return true;
if (!(sender instanceof Player) && args.length < 1) {
sender.sendMessage(ChatColor.RED + "You need to specify the player! " + ChatColor.YELLOW + "/rtp [player] [world]");
return true;
}
Player p = (Player) sender;
if (args.length >= 1) {
p = Bukkit.getServer().getPlayer(args[0]);
if (p != sender && !permissionMissingCheck(sender, this.getPermission() + ".others")) {
return true;
} else if (p == null) {
sender.sendMessage(ChatColor.RED + "Player not found!");
return true;
}
}
World w = p.getWorld();
if (args.length >= 2) {
w = Bukkit.getServer().getWorld(args[1]);
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 = telCooldown.timeLeftSeconds(p);
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;
}
telCooldown.resetPlayer(p);
randomTeleport(w, p);
return true;
}
@Override
public String getPermission() {
return getPermissionPath();
}
});
}
public void randomTeleport(World w, Player p) {
Location loc = null;
for (int i = 0; i < MAX_TRIES && loc == null; i++) {
loc = generateLoc(w);
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) {
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) {
int wboff = 16;
Location loc = w.getWorldBorder().getCenter();
int radius = (int) w.getWorldBorder().getSize() / 2;
int minx = loc.getBlockX() - radius;
int minz = loc.getBlockZ() - radius;
int maxx = loc.getBlockX() + radius;
int maxz = loc.getBlockZ() + radius;
if (wboff >= radius)
wboff = 0;
int rangex = (maxx - minx) - wboff * 2;
int rangez = (maxz - minz) - wboff * 2;
int finx = minx + (int) (Math.random() * rangex) + wboff;
int finz = minz + (int) (Math.random() * rangez) + wboff;
Location targetloc = w.getHighestBlockAt(finx, finz).getLocation();
targetloc.subtract(0d, 1d, 0d);
return targetloc;
}
public boolean canTeleportHere(Location loc, Player p, boolean allowAir) {
Block block = loc.getWorld().getBlockAt(loc);
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())
return false;
try {
if (!com.sk89q.worldguard.bukkit.WGBukkit.getPlugin().canBuild(p, block)) {
return false;
}
} catch (NoClassDefFoundError e) {
}
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;
}
}