use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.
the class CommandManager method onSetFloor.
/**
* Called whenever a player wants to set the floor of a plot
*
* @param player the player
* @param arena the arena
* @param id the plot id to set the floor of
* @since 5.8.0
*/
@Subcommand("setfloor")
@Description("Set the floor of a plot (inclusive)")
@CommandPermission("bg.setfloor")
@CommandCompletion("@arenas @nothing")
public void onSetFloor(Player player, Arena arena, int id) {
final var plot = arena.getPlot(id);
if (plot == null) {
MessageManager.getInstance().send(player, ChatColor.RED + "That's not a valid plot");
return;
}
player.getInventory().setItemInMainHand(new ItemBuilder(player, Material.STICK).setDisplayName(ChatColor.LIGHT_PURPLE + "Wand").setClickEvent(new Consumer<>() {
private Location previousLocation;
@Override
public void accept(PlayerInteractEvent event) {
YamlConfiguration arenas = SettingsManager.getInstance().getArenas();
var player = event.getPlayer();
var action = event.getAction();
if (action != Action.LEFT_CLICK_BLOCK && action != Action.RIGHT_CLICK_BLOCK)
return;
if (previousLocation == null) {
previousLocation = event.getClickedBlock().getLocation();
MessageManager.getInstance().send(player, ChatColor.GREEN + "Now click on the other corner");
} else {
// second time
var location = event.getClickedBlock().getLocation();
String name = arena.getName();
int plotID = plot.getId();
World world = location.getWorld();
if (previousLocation.getWorld().equals(world)) {
arenas.set(name + '.' + plotID + ".floor.high.world", world.getName());
arenas.set(name + '.' + plotID + ".floor.low.world", previousLocation.getWorld().getName());
} else {
MessageManager.getInstance().send(player, ChatColor.RED + "The world has to be the same");
event.setCancelled(true);
return;
}
int highestX = Math.max(previousLocation.getBlockX(), location.getBlockX());
int lowestX = Math.min(previousLocation.getBlockX(), location.getBlockX());
int highestY = Math.max(previousLocation.getBlockY(), location.getBlockY());
int lowestY = Math.min(previousLocation.getBlockY(), location.getBlockY());
int highestZ = Math.max(previousLocation.getBlockZ(), location.getBlockZ());
int lowestZ = Math.min(previousLocation.getBlockZ(), location.getBlockZ());
// x
arenas.set(name + '.' + plotID + ".floor.high.x", highestX);
arenas.set(name + '.' + plotID + ".floor.low.x", lowestX);
// y
arenas.set(name + '.' + plotID + ".floor.high.y", highestY);
arenas.set(name + '.' + plotID + ".floor.low.y", lowestY);
// z
arenas.set(name + '.' + plotID + ".floor.high.z", highestZ);
arenas.set(name + '.' + plotID + ".floor.low.z", lowestZ);
SettingsManager.getInstance().save();
String worldName = world.getName();
Supplier<World> worldSupplier = () -> Bukkit.getWorld(worldName);
Region region = RegionFactory.createRegion(worldSupplier, highestX, highestY, highestZ, lowestX, lowestY, lowestZ);
plot.setFloor(region);
MessageManager.getInstance().send(player, ChatColor.GREEN + "Floor set!");
previousLocation = null;
player.getInventory().setItemInMainHand(null);
}
event.setCancelled(true);
}
}).build());
MessageManager.getInstance().send(player, ChatColor.GREEN + "Please click on one corner");
}
use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.
the class Move method teleportBack.
/**
* Teleport a entity back to the specified plot
*
* @param plot the plot to teleport the entity to
* @param entity the entity to teleport
*
* @since 7.0.0
*/
private static void teleportBack(Plot plot, Entity entity, Location previousLocation, Location currentLocation) {
YamlConfiguration messages = SettingsManager.getInstance().getMessages();
if (plot == null)
return;
Region firstBoundary = plot.getBoundary();
if (!firstBoundary.isInside(previousLocation)) {
List<Block> allBlocks = firstBoundary.getAllBlocks();
entity.teleport(allBlocks.get(ThreadLocalRandom.current().nextInt(allBlocks.size())).getLocation());
} else if (!firstBoundary.isInside(currentLocation)) {
entity.teleport(previousLocation);
MessageManager.getInstance().send(entity, messages.getStringList("in-game.move-out-bounds"));
}
}
use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.
the class TreeGrow method onStructureGrow.
/**
* Handles trees growing
*
* @param e an event representing a structure growing
* @see StructureGrowEvent
* @since 2.1.0
*/
@EventHandler
public static void onStructureGrow(StructureGrowEvent e) {
Plot plot = null;
for (var arena : ArenaManager.getInstance().getArenas()) {
for (Plot p : arena.getPlots()) {
Region boundary = p.getBoundary();
if (boundary == null)
continue;
if (boundary.isInside(e.getLocation())) {
plot = p;
break;
}
}
}
if (plot == null)
return;
Region boundary = plot.getBoundary();
if (boundary == null)
return;
e.getBlocks().removeIf(blockState -> !boundary.isInside(blockState.getLocation()));
}
use of com.gmail.stefvanschiedev.buildinggame.utils.region.Region in project buildinggame by stefvanschie.
the class Arena method setVotingPlot.
/**
* Sets the new voting plot, send messages and titles, teleports all players, gives them new vote items and changes
* their time and weather.
*
* @param votingPlot the new voting plot
* @see Plot
* @since 2.1.0
*/
public void setVotingPlot(Plot votingPlot) {
SettingsManager instance = SettingsManager.getInstance();
YamlConfiguration config = instance.getConfig();
YamlConfiguration messages = instance.getMessages();
this.votingPlot = votingPlot;
Region boundary = votingPlot.getBoundary();
Location safeLocation = boundary.getSafeLocation();
Location center = boundary.getCenter();
// https://bukkit.org/threads/lookat-and-move-functions.26768/
if (safeLocation != null) {
// Clone the loc to prevent applied changes to the input loc
safeLocation = safeLocation.clone();
// Values of change in distance (make it relative)
double dx = center.getX() - safeLocation.getX();
double dz = center.getZ() - safeLocation.getZ();
// Set yaw
if (dx != 0)
safeLocation.setYaw((dx < 0 ? ((float) (1.5 * Math.PI)) : ((float) (0.5 * Math.PI))) - (float) Math.atan(dz / dx));
else if (dz < 0)
safeLocation.setYaw((float) Math.PI);
// Set pitch
safeLocation.setPitch((float) -Math.atan((center.getY() - safeLocation.getY()) / (Math.sqrt(Math.pow(dx, 2) + Math.pow(dz, 2)))));
// Set values, convert to degrees (invert the yaw since Bukkit uses a different yaw dimension format)
safeLocation.setYaw(-safeLocation.getYaw() * 180f / (float) Math.PI);
safeLocation.setPitch(safeLocation.getPitch() * 180f / (float) Math.PI);
}
for (Plot plot : getUsedPlots()) {
for (GamePlayer gamePlayer : plot.getAllGamePlayers()) {
Player player = gamePlayer.getPlayer();
if (!config.getBoolean("names-after-voting")) {
messages.getStringList("voting.message").forEach(message -> MessageManager.getInstance().send(player, message.replace("%playerplot%", votingPlot.getPlayerFormat())));
gamePlayer.addTitleAndSubtitle(messages.getString("voting.title").replace("%playerplot%", votingPlot.getPlayerFormat()), messages.getString("voting.subtitle").replace("%playerplot%", votingPlot.getPlayerFormat()));
gamePlayer.sendActionbar(messages.getString("voting.actionbar").replace("%playerplot%", votingPlot.getPlayerFormat()));
}
int blockIndex = ThreadLocalRandom.current().nextInt(votingPlot.getBoundary().getAllBlocks().size());
player.teleport(Objects.requireNonNullElseGet(safeLocation, () -> boundary.getAllBlocks().get(blockIndex).getLocation()));
// give blocks
player.getInventory().clear();
if (gamePlayer.getGamePlayerType() == GamePlayerType.PLAYER) {
config.getConfigurationSection("voting.items").getKeys(false).forEach(identifier -> {
boolean save = false;
if (!messages.contains("voting.items." + identifier + ".name")) {
messages.set("voting.items." + identifier + ".name", "Type: Null");
save = true;
}
if (!messages.contains("voting.items." + identifier + ".lore")) {
messages.set("voting.items." + identifier + ".lore", new ArrayList<String>(0));
save = true;
}
if (save)
instance.save();
Material material = SettingsManager.getInstance().getMaterial("voting.items." + identifier + ".id", Material.BARRIER);
player.getInventory().setItem(config.getInt("voting.items." + identifier + ".slot") - 1, new ItemBuilder(player, material).setDisplayName(MessageManager.translate(messages.getString("voting.items." + identifier + ".name"))).setLore(MessageManager.translate(messages.getStringList("voting.items." + identifier + ".lore"))).movable(false).setClickEvent(event -> {
int points = config.getInt("voting.items." + identifier + ".points");
votingPlot.addVote(new Vote(points, player));
event.setCancelled(true);
}).build());
});
boolean worldEditEnabled = Bukkit.getPluginManager().isPluginEnabled("WorldEdit");
if (worldEditEnabled && config.getBoolean("reports.enable")) {
player.getInventory().setItem(8, new ItemBuilder(player, Material.BOOK).setDisplayName(ChatColor.RED + "Report build").movable(false).setClickEvent(event -> {
var dateTimeFormatter = DateTimeFormatter.ofPattern("yyy-MM-dd_HH-mm-ss");
String players = getVotingPlot().getGamePlayers().stream().map(gp -> '-' + gp.getPlayer().getName()).reduce("", String::concat);
var fileName = LocalDateTime.now().format(dateTimeFormatter) + players + ".schem";
var file = new File(instance.getReportsSchematicsFolder(), fileName);
getVotingPlot().getBoundary().saveSchematic(file, () -> MessageManager.getInstance().send(event.getPlayer(), ChatColor.GREEN + "Your report has been saved."));
getVotingPlot().getGamePlayers().forEach(gp -> Report.add(new Report(gp.getPlayer(), player, ZonedDateTime.now(), file)));
}).build());
}
}
// update scoreboard and update time and weather
if (config.getBoolean("scoreboards.vote.enable"))
getVoteScoreboard(plot).show(player);
player.setPlayerTime(plot.getTime(), false);
player.setPlayerWeather(plot.isRaining() ? WeatherType.DOWNFALL : WeatherType.CLEAR);
}
}
getVotedPlots().add(votingPlot);
}
Aggregations