Skip to content

Commit

Permalink
Added /terraform <Height>
Browse files Browse the repository at this point in the history
  • Loading branch information
Le4nderS committed Jan 15, 2022
1 parent 7b979b4 commit db9d8f3
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 18 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.leander</groupId>
<artifactId>bteg-utilities</artifactId>
<version>1.3-SNAPSHOT</version>
<version>1.4-SNAPSHOT</version>
<packaging>jar</packaging>

<name>BTEG Utilities</name>
Expand Down Expand Up @@ -97,6 +97,10 @@
<id>headdatabase-api</id>
<url>https://mvn.intellectualsites.com/content/repositories/thirdparty/</url>
</repository>
<repository>
<id>nachwahl-repo</id>
<url>https://maven.nachwahl.dev/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -117,5 +121,11 @@
<artifactId>HeadDatabase-API</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>6.1.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
4 changes: 4 additions & 0 deletions src/main/java/de/leander/bteggamemode/BTEGGamemode.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.leander.bteggamemode;

import de.leander.bteggamemode.commands.GuessGuessing;
import de.leander.bteggamemode.commands.Speed;
import de.leander.bteggamemode.commands.Terraform;
import de.leander.bteggamemode.events.BedrockTerraBlock;
import de.leander.bteggamemode.events.JoinMessage;
import de.leander.bteggamemode.events.SetGamemode;
Expand All @@ -16,6 +18,8 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new JoinMessage(), this);
getServer().getPluginManager().registerEvents(new BedrockTerraBlock(), this);
getCommand("bteg").setExecutor(new RestartTimer(this));
getCommand("terraform").setExecutor(new Terraform(this));
getCommand("speed").setExecutor(new Speed(this));
this.getServer().getMessenger().registerOutgoingPluginChannel(this, "BungeeCord");
getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "[BTEG Gamemode]: Plugin enabled!");
}
Expand Down
23 changes: 7 additions & 16 deletions src/main/java/de/leander/bteggamemode/commands/RestartTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class RestartTimer implements CommandExecutor {

public static int timeleft = 120;
private static Plugin plugin;
BukkitRunnable runnable;


public RestartTimer(JavaPlugin pPlugin) {
Expand All @@ -40,18 +41,16 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
if(args.length == 0) {
player.sendMessage("§b§lBTEG §7» §7Example commands:");
player.sendMessage("§b§lBTEG §7» §7/bteg restart <Seconds[5min=300]> - Restarts a server - Standard: §82 minutes");
player.sendMessage("§b§lBTEG §7» §7/terraform <Height>");
}
else if(args.length == 1 || args.length == 2){


if(args[0].matches("restart")) {
if(args.length == 2){


timeleft = Integer.parseInt(args[1]);

timeleft = Integer.parseInt(args[1]);
}
BukkitRunnable runnable = new BukkitRunnable() {
runnable = new BukkitRunnable() {
@Override
public void run() {
for (Player p : Bukkit.getOnlinePlayers()) {
Expand All @@ -69,21 +68,13 @@ public void run() {
}
timeleft--;
if(timeleft == 0){
Bukkit.getServer().shutdown();
Bukkit.getServer().spigot().restart();
}
}
};
player.sendMessage("§b§lBTEG §7» §7Planned restart in §4"+shortInteger(timeleft) +"§7 hours.");
runnable.runTaskTimer(plugin, 40,20);

if(args[1].matches("stop") || args[1].matches("cancel")) {
runnable.cancel(); //Das funktioniert noch nicht
for (Player p : Bukkit.getOnlinePlayers()) {
p.sendMessage("§b§lBTEG §7» §4aRestart of " + Bukkit.getServerName() + " canceled!");
p.spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(""));
}
return false;
}

}
else if(args[0].matches("warp")) {
player.chat("/nwarp");
Expand All @@ -104,7 +95,7 @@ else if(args.length > 1){



return false;
return true;
}

void sendMessage(int pZeit){
Expand Down
242 changes: 242 additions & 0 deletions src/main/java/de/leander/bteggamemode/commands/Terraform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
package de.leander.bteggamemode.commands;

import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.bukkit.BukkitUtil;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import com.sk89q.worldedit.function.mask.BlockMask;
import com.sk89q.worldedit.function.operation.ForwardExtentCopy;
import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.function.operation.Operations;
import com.sk89q.worldedit.regions.Polygonal2DRegion;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.schematic.SchematicFormat;
import com.sk89q.worldedit.session.ClipboardHolder;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.biome.BaseBiome;
import com.sk89q.worldedit.world.registry.*;
import de.leander.bteggamemode.BTEGGamemode;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.List;

public class Terraform implements CommandExecutor {

private static int height;
static World world1;
private static Plugin plugin;
private static boolean runterterraformen;
private static Polygonal2DRegion polyRegion;
static CuboidClipboard clipboard;
static ClipboardHolder clipboardHolder;

static Clipboard backup;
static BlockVector koordinaten;

public Terraform(JavaPlugin pPlugin) {
plugin = pPlugin;
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) { return true; }
Player player = (Player) sender;
if (command.getName().equalsIgnoreCase("terraform")) {
if (player.hasPermission("bteg.terraform")) {
if (args[0].equals("undo")) {
load(player);
return true;
} else {
height = (Integer.parseInt(args[0]) - 1);
}
try {
terraform(player);
} catch (MaxChangedBlocksException | EmptyClipboardException e) {
e.printStackTrace();
}

world1 = player.getWorld();
return true;
}
} return true;
}

void terraform(Player player) throws MaxChangedBlocksException, EmptyClipboardException {
Region plotRegion;
// Get WorldEdit selection of player
try {
plotRegion = WorldEdit.getInstance().getSessionManager().findByName(player.getName()).getSelection(WorldEdit.getInstance().getSessionManager().findByName(player.getName()).getSelectionWorld());
} catch (NullPointerException | IncompleteRegionException ex) {
ex.printStackTrace();
player.sendMessage("§7§l>> §cPlease select a WorldEdit selection!");
return;
}
try {
// Check if WorldEdit selection is polygonal
if (plotRegion instanceof Polygonal2DRegion) {
// Cast WorldEdit region to polygonal region
polyRegion = (Polygonal2DRegion) plotRegion;
if (polyRegion.getLength() > 100 || polyRegion.getWidth() > 100 || polyRegion.getHeight() > 30) {
player.sendMessage("§7§l>> §cPlease adjust your selection size!");
return;
}
// Set minimum selection height under player location
polyRegion.setMinimumY(height);
polyRegion.setMaximumY(height + 35);

} else {
player.sendMessage("§7§l>> §cPlease use poly selection to terraform!");
return;
}
} catch (Exception ex) {
player.sendMessage("§7§l>> §cAn error occurred while selection area!");
return;
}

replaceEmerald(polyRegion, player);

player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 1);

}

private static void replaceEmerald(Region region, Player player) throws MaxChangedBlocksException, EmptyClipboardException {
world1 = player.getWorld();
Vector centerBlock = region.getCenter();
runterterraformen = height < player.getWorld().getHighestBlockAt(centerBlock.getBlockX(), centerBlock.getBlockZ()).getY() - 1;
if ( region.getHeight() > 50){
player.sendMessage("§b§lBTEG §7» §7You cannot terraform areas with height more than 50 blocks difference!");
return;
}else {
player.sendMessage("§b§lBTEG §7» §7Terraforming started. Please wait a short moment!");
// player.performCommand("/copy"); // Am anfang benutzt aber speichern des clipboards funktioniert jetzt nur mit der worldedit api
//WorldEdit CLipboard backup
backup(polyRegion, player);
koordinaten = BlockVector.toBlockPoint(region.getMinimumPoint().getBlockX(),region.getMinimumPoint().getY(),region.getMinimumPoint().getZ());
// backup.setOrigin(koordinaten);
//

for (int i = region.getMinimumPoint().getBlockX(); i <= region.getMaximumPoint().getBlockX(); i++) {
for (int j = region.getMinimumPoint().getBlockY(); j <= region.getMaximumPoint().getBlockY(); j++) {
for (int k = region.getMinimumPoint().getBlockZ(); k <= region.getMaximumPoint().getBlockZ(); k++) {
if (region.contains(new Vector(i, j, k))) {
Block block = world1.getBlockAt(i, j, k);
if (block.getType().equals(Material.GRASS_PATH)) {
block.setType(Material.LAPIS_BLOCK);
}


if (runterterraformen) {
for (int z = j; z >= height; z--) {
if (block.getLocation().getBlockY() == height + 1) {
world1.getBlockAt(i, height, k).setType(Material.EMERALD_BLOCK);
}
if (block.getType().equals(Material.LAPIS_BLOCK)) {
world1.getBlockAt(i, z, k).setType(Material.LAPIS_BLOCK);
}
if (block.getType().equals(Material.CONCRETE)) {
world1.getBlockAt(i, z, k).setType(Material.CONCRETE);
world1.getBlockAt(i, z, k).setData((byte) 7);
}
if (block.getType().equals(Material.BRICK)) {
world1.getBlockAt(i, z, k).setTypeId(45, false);
}
}
for (int z = j; z > height; z--) {
if (block.getLocation().getBlockY() > height + 2) {
world1.getBlockAt(i, z, k).setType(Material.AIR);
}
}
}

if (!runterterraformen) {
for (int z = j; z >= region.getMinimumPoint().getBlockY(); z--) {
if (block.getLocation().getBlockY() == (region.getMinimumPoint().getBlockY())) {
if (block.getType().equals(Material.LAPIS_BLOCK) || block.getType().equals(Material.CONCRETE) || block.getType().equals(Material.BRICK)) {
} else {
world1.getBlockAt(i, z, k).setType(Material.EMERALD_BLOCK);
}
}
}
for (int z = j; z <= height; z++) {
if (block.getLocation().getBlockY() < height) {
if (block.getType().equals(Material.EMERALD_BLOCK)) {
world1.getBlockAt(i, z, k).setType(Material.EMERALD_BLOCK);
}
if (block.getType().equals(Material.LAPIS_BLOCK)) {
world1.getBlockAt(i, z, k).setType(Material.LAPIS_BLOCK);
}
if (block.getType().equals(Material.CONCRETE)) {
world1.getBlockAt(i, z, k).setType(Material.CONCRETE);
world1.getBlockAt(i, z, k).setData((byte) 7);
}
if (block.getType().equals(Material.BRICK)) {
world1.getBlockAt(i, z, k).setTypeId(45, false);
}
}
}
}
}
}
}
}
player.sendMessage("§b§lBTEG §7» §7Area succesfully terraformed to height §l"+(height+1)+"! Type </terraform undo> for undo.");
}
}

private static void backup(Region pRegion,Player player){
WorldEditPlugin worldEdit = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
WorldEdit we = worldEdit.getWorldEdit();

WorldData data = polyRegion.getWorld().getWorldData();
backup = new BlockArrayClipboard(pRegion);

LocalPlayer localPlayer = worldEdit.wrapPlayer(player);
LocalSession localSession = we.getSession(localPlayer);
ClipboardHolder selection = new ClipboardHolder(backup, data); //localSession.getClipboard();
EditSession editSession = localSession.createEditSession(localPlayer);

Vector min = selection.getClipboard().getMinimumPoint();
Vector max = selection.getClipboard().getMaximumPoint();

editSession.enableQueue();
clipboard = new CuboidClipboard(max.subtract(min).add(new Vector(1, 1, 1)), min);
clipboard.copy(editSession);
editSession.flushQueue();


}

private void load(Player player) {
try {
//
EditSession editSession = new EditSession(new BukkitWorld(player.getWorld()), -1);
editSession.enableQueue();

clipboard.paste(editSession, koordinaten,false,true);
editSession.flushQueue();

player.sendMessage("§b§lBTEG §7» §7Undo succesful!");

} catch (WorldEditException exception) {
exception.printStackTrace();
}

}
}
8 changes: 7 additions & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ website: bte-germany.de
commands:
bteg:
description: Restarts the server within 2 minutes
usage: /bteg restart
usage: /bteg restart
terraform:
description: Terraforms an area for you
usage: /terraform <Height>
speed:
description: Gives you speed
usage: /speed <Number>

0 comments on commit db9d8f3

Please sign in to comment.