Skip to content

Commit

Permalink
Rework border (untested)
Browse files Browse the repository at this point in the history
  • Loading branch information
Almighty-Satan committed Sep 28, 2024
1 parent b5619a8 commit 393352d
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 305 deletions.
6 changes: 3 additions & 3 deletions src/de/cuuky/varo/command/essentials/BorderCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public boolean onCommand(CommandSender sender, Command arg1, String arg2, String
}

if (args.length == 0) {
sender.sendMessage(Main.getPrefix() + ConfigMessages.COMMANDS_BORDER_SIZE.getValue(vp).replace("%size%", String.valueOf((sender instanceof Player ? Main.getVaroGame().getVaroWorldHandler().getVaroWorld(((Player) sender).getWorld()).getVaroBorder().getBorderSize() : Main.getVaroGame().getVaroWorldHandler().getMainWorld().getVaroBorder().getBorderSize()))));
sender.sendMessage(Main.getPrefix() + ConfigMessages.COMMANDS_BORDER_SIZE.getValue(vp).replace("%size%", String.valueOf((sender instanceof Player ? Main.getVaroGame().getVaroWorldHandler().getVaroWorld(((Player) sender).getWorld()).getVaroBorder().getSize() : Main.getVaroGame().getVaroWorldHandler().getMainWorld().getVaroBorder().getSize()))));
if (sender instanceof Player)
sender.sendMessage(Main.getPrefix() + ConfigMessages.COMMANDS_BORDER_DISTANCE.getValue(vp).replace("%distance%", String.valueOf((int) Main.getVaroGame().getVaroWorldHandler().getVaroWorld(((Player) sender).getWorld()).getVaroBorder().getBorderDistanceTo((Player) sender))));
sender.sendMessage(Main.getPrefix() + ConfigMessages.COMMANDS_BORDER_DISTANCE.getValue(vp).replace("%distance%", String.valueOf((int) Main.getVaroGame().getVaroWorldHandler().getVaroWorld(((Player) sender).getWorld()).getVaroBorder().getDistance((Player) sender))));
if (sender.hasPermission("varo.setup")) {
sender.sendMessage(Main.getPrefix() + ConfigMessages.COMMANDS_BORDER_USAGE.getValue(vp));
}
Expand All @@ -38,7 +38,7 @@ public boolean onCommand(CommandSender sender, Command arg1, String arg2, String

if (args[0].equalsIgnoreCase("center")) {
if (p != null) {
worldHandler.getVaroWorld(p.getWorld()).getVaroBorder().setBorderCenter(p.getLocation());
worldHandler.getVaroWorld(p.getWorld()).getVaroBorder().setCenter(p.getLocation());
p.sendMessage(Main.getPrefix() + "Zentrum der Border gesetzt!");
} else sender.sendMessage("Only for players!");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import de.cuuky.varo.Main;
import de.cuuky.varo.configuration.configurations.language.languages.ConfigMessages;
import de.cuuky.varo.entity.player.VaroPlayer;
import de.cuuky.varo.game.world.border.VaroWorldBorder;
import de.cuuky.varo.game.world.border.VaroBorder;

public class SetWorldspawnCommand implements CommandExecutor {

Expand All @@ -34,9 +34,9 @@ public boolean onCommand(CommandSender sender, Command arg1, String arg2, String
}

p.getWorld().setSpawnLocation(p.getLocation().getBlockX(), p.getLocation().getBlockY(), p.getLocation().getBlockZ());
VaroWorldBorder border = Main.getVaroGame().getVaroWorldHandler().getVaroWorld(p.getWorld()).getVaroBorder();
VaroBorder border = Main.getVaroGame().getVaroWorldHandler().getVaroWorld(p.getWorld()).getVaroBorder();
if(border != null)
border.setBorderCenter(p.getLocation());
border.setCenter(p.getLocation());

p.sendMessage(Main.getPrefix() + Main.getColorCode() + ConfigMessages.COMMANDS_SETWORLDSPAWN.getValue(vp));
p.playSound(p.getLocation(), XSound.BLOCK_NOTE_BLOCK_BASEDRUM.parseSound(), 1, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import de.cuuky.varo.entity.player.VaroPlayer;
import de.cuuky.varo.entity.player.disconnect.VaroPlayerDisconnect;
import de.cuuky.varo.utils.VaroUtils;
import de.varoplugin.cfw.version.ServerVersion;
import de.varoplugin.cfw.version.VersionUtils;

public class MessagePlaceholderLoader {
Expand Down Expand Up @@ -69,10 +68,10 @@ private void loadMessagePlaceHolder() {

private void loadPlayerPlaceholder() {
new VaroPlayerMessagePlaceholder("distanceToBorder", 1, "Ersetzt durch die Distanz zur Border des Spielers", (player) -> {
if (!VersionUtils.getVersion().isHigherThan(ServerVersion.ONE_7) || Main.getVaroGame() == null)
if (Main.getVaroGame() == null)
return "0";

return String.valueOf((int) Main.getVaroGame().getVaroWorldHandler().getVaroWorld(player.getPlayer().getWorld()).getVaroBorder().getBorderDistanceTo(player.getPlayer()));
return String.valueOf((int) Main.getVaroGame().getVaroWorldHandler().getVaroWorld(player.getPlayer().getWorld()).getVaroBorder().getDistance(player.getPlayer()));
});

new VaroPlayerMessagePlaceholder("seconds", 1, "Ersetzt durch den Countdown des Spielers", (player) -> String.valueOf(player.getStats().getCountdown()));
Expand Down
27 changes: 16 additions & 11 deletions src/de/cuuky/varo/game/world/VaroWorld.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
package de.cuuky.varo.game.world;

import de.varoplugin.cfw.utils.JavaUtils;
import de.varoplugin.cfw.version.ServerVersion;
import de.varoplugin.cfw.version.VersionUtils;
import de.cuuky.varo.Main;
import de.cuuky.varo.configuration.configurations.config.ConfigSetting;
import de.cuuky.varo.game.world.border.VaroWorldBorder;
import java.util.ArrayList;
import java.util.List;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import de.cuuky.varo.Main;
import de.cuuky.varo.configuration.configurations.config.ConfigSetting;
import de.cuuky.varo.game.world.border.DefaultVaroBorder;
import de.cuuky.varo.game.world.border.NopVaroBorder;
import de.cuuky.varo.game.world.border.VaroBorder;
import de.varoplugin.cfw.utils.JavaUtils;
import de.varoplugin.cfw.version.ServerVersion;
import de.varoplugin.cfw.version.VersionUtils;

public class VaroWorld {

private World world;
private VaroWorldBorder varoWorldBorder;
private VaroBorder varoWorldBorder;

public VaroWorld(World world) {
this.world = world;
Expand All @@ -30,7 +33,9 @@ public VaroWorld(World world) {
}

if (VersionUtils.getVersion().isHigherThan(ServerVersion.ONE_7))
this.varoWorldBorder = new VaroWorldBorder(world);
this.varoWorldBorder = new DefaultVaroBorder(world);
else
this.varoWorldBorder = new NopVaroBorder(world);
}

private List<Block> getBlocksBetweenPoints(Location l1, Location l2) {
Expand Down Expand Up @@ -87,7 +92,7 @@ public World getWorld() {
return this.world;
}

public VaroWorldBorder getVaroBorder() {
public VaroBorder getVaroBorder() {
return this.varoWorldBorder;
}
}
95 changes: 59 additions & 36 deletions src/de/cuuky/varo/game/world/VaroWorldHandler.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package de.cuuky.varo.game.world;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;

import de.varoplugin.cfw.version.ServerVersion;
import de.varoplugin.cfw.version.VersionUtils;
import de.cuuky.varo.Main;
import de.cuuky.varo.configuration.configurations.config.ConfigSetting;
import de.cuuky.varo.configuration.configurations.language.languages.ConfigMessages;
import de.cuuky.varo.game.world.border.decrease.BorderDecrease;
import de.cuuky.varo.game.world.border.decrease.DecreaseReason;
import de.cuuky.varo.logger.logger.EventLogger.LogType;
import de.varoplugin.cfw.version.ServerVersion;
import de.varoplugin.cfw.version.VersionUtils;

public class VaroWorldHandler {

private VaroWorld mainVaroWorld;
private ArrayList<VaroWorld> worlds;
private final VaroWorld mainVaroWorld;
private final ArrayList<VaroWorld> worlds = new ArrayList<>();
private final Queue<BorderDecrease> borderDecreases = new LinkedList<>();
private BorderDecrease activeBorderDecrease;

public VaroWorldHandler() {
World mainworld = Bukkit.getWorld(VersionUtils.getVersionAdapter().getServerProperties().getProperty("level-name"));
this.mainVaroWorld = new VaroWorld(mainworld);

this.worlds = new ArrayList<>();
this.worlds.add(mainVaroWorld);

for (World world : Bukkit.getWorlds())
Expand All @@ -49,29 +53,67 @@ public void addWorld(World world) {
VaroWorld vworld = new VaroWorld(world);
this.worlds.add(vworld);

if (VersionUtils.getVersion().isHigherThan(ServerVersion.ONE_7) && ConfigSetting.WORLD_SNCHRONIZE_BORDER.getValueAsBoolean())
vworld.getVaroBorder().setBorderSize(getBorderSize(), 0);
if (ConfigSetting.WORLD_SNCHRONIZE_BORDER.getValueAsBoolean())
vworld.getVaroBorder().setSize(getBorderSize(), 0);
}

public void decreaseBorder(DecreaseReason reason) {
if (!VersionUtils.getVersion().isHigherThan(ServerVersion.ONE_7) || !reason.isEnabled())
if (!reason.isEnabled())
return;

BorderDecrease decr = new BorderDecrease(reason.getSize(), reason.getDecreaseSpeed());
decr.setStartHook(() -> reason.postBroadcast());
decr.setFinishHook(() -> reason.postAlert());
if (!isBorderMinimumSize()) {
this.borderDecreases.add(new BorderDecrease(reason.getSize(), reason.getDecreaseSpeed(), reason));
runBorderCheck();
}
}

public void setBorderSize(double size, long time, World world) {
if (!VersionUtils.getVersion().isHigherThan(ServerVersion.ONE_7))

private boolean isBorderMinimumSize() {
return this.getBorderSize() <= ConfigSetting.MIN_BORDER_SIZE.getValueAsInt();
}

private void runBorderCheck() {
if (this.activeBorderDecrease != null || this.borderDecreases.isEmpty())
return;
this.activeBorderDecrease = this.borderDecreases.poll();
this.activeBorderDecrease.getReason().postBroadcast();

double size = this.getBorderSize();
double newSize = Math.max(size - this.activeBorderDecrease.getAmount(), ConfigSetting.MIN_BORDER_SIZE.getValueAsInt());
double diff = Math.abs(size - newSize);
long time = (long) (diff / this.activeBorderDecrease.getSpeed());
this.setBorderSize(newSize, time, null);

Bukkit.getScheduler().runTaskLater(Main.getInstance(), () -> {
this.activeBorderDecrease.getReason().postAlert();
this.activeBorderDecrease = null;

if (this.isBorderMinimumSize()) {
Main.getLanguageManager().broadcastMessage(ConfigMessages.BORDER_MINIMUM_REACHED);
this.borderDecreases.clear();
} else
this.runBorderCheck();
}, time * 20L);
}

public double getBorderSize(World world) {
if (ConfigSetting.WORLD_SNCHRONIZE_BORDER.getValueAsBoolean())
return getBorderSize();

VaroWorld vworld = world != null ? getVaroWorld(world) : this.mainVaroWorld;
return vworld.getVaroBorder().getSize();
}

public double getBorderSize() {
return this.mainVaroWorld.getVaroBorder().getSize();
}

public void setBorderSize(double size, long time, World world) {
if (ConfigSetting.WORLD_SNCHRONIZE_BORDER.getValueAsBoolean())
for (VaroWorld vworld : worlds)
vworld.getVaroBorder().setBorderSize(size, time);
vworld.getVaroBorder().setSize(size, time);
else {
VaroWorld vworld = world != null ? getVaroWorld(world) : mainVaroWorld;
vworld.getVaroBorder().setBorderSize(size, time);
vworld.getVaroBorder().setSize(size, time);
}
}

Expand All @@ -80,26 +122,7 @@ public VaroWorld getVaroWorld(World world) {
if (vworld.getWorld().equals(world))
return vworld;

throw new NullPointerException("Couldn't find VaroWorld for " + world.getName());
}

public double getBorderSize(World world) {
if (ConfigSetting.WORLD_SNCHRONIZE_BORDER.getValueAsBoolean())
return getBorderSize();
else {
if (!VersionUtils.getVersion().isHigherThan(ServerVersion.ONE_7))
return 0;

VaroWorld vworld = world != null ? getVaroWorld(world) : this.mainVaroWorld;
return vworld.getVaroBorder().getBorderSize();
}
}

public double getBorderSize() {
if (!VersionUtils.getVersion().isHigherThan(ServerVersion.ONE_7))
return 0;

return this.mainVaroWorld.getVaroBorder().getBorderSize();
throw new IllegalArgumentException("Couldn't find VaroWorld " + world.getName());
}

public ArrayList<VaroWorld> getWorlds() {
Expand Down
33 changes: 33 additions & 0 deletions src/de/cuuky/varo/game/world/border/DefaultVaroBorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.cuuky.varo.game.world.border;

import org.bukkit.Location;
import org.bukkit.World;

public class DefaultVaroBorder implements RectangularVaroBorder {

private final World world;

public DefaultVaroBorder(World world) {
this.world = world;
}

@Override
public double getSize() {
return this.world.getWorldBorder().getSize();
}

@Override
public void setSize(double size, long time) {
this.world.getWorldBorder().setSize(size, time);
}

@Override
public Location getCenter() {
return this.world.getWorldBorder().getCenter();
}

@Override
public void setCenter(Location location) {
this.world.getWorldBorder().setCenter(location);
}
}
61 changes: 61 additions & 0 deletions src/de/cuuky/varo/game/world/border/NopVaroBorder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* VaroPlugin
* Copyright (C) 2022 Cuuky, Almighty-Satan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.cuuky.varo.game.world.border;

import org.bukkit.Location;
import org.bukkit.World;

public class NopVaroBorder implements VaroBorder {

private final World world;

public NopVaroBorder(World world) {
this.world = world;
}

@Override
public double getSize() {
return 0;
}

@Override
public void setSize(double size, long time) {
// nop
}

@Override
public Location getCenter() {
return new Location(this.world, 0, 0, 0);
}

@Override
public void setCenter(Location location) {
// nop
}

@Override
public double getDistance(Location location) {
return 0;
}

@Override
public boolean isOutside(Location location) {
return false;
}
}
Loading

0 comments on commit 393352d

Please sign in to comment.