Skip to content

Commit

Permalink
Makes barrier more blocking. Makes border on by default.
Browse files Browse the repository at this point in the history
Teleports players back a bit if they go over the barrier.

#51

#52
  • Loading branch information
tastybento committed Jan 10, 2021
1 parent adb21fc commit d0f2421
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/main/java/world/bentobox/border/Border.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public final class Border extends Addon {

private boolean hooked;

private Config<Settings> config = new Config<>(this, Settings.class);

@Override
public void onLoad() {
// Save default config.yml
Expand All @@ -24,8 +26,6 @@ public void onLoad() {

@Override
public void onEnable() {


// Register commands
getPlugin().getAddonsManager().getGameModeAddons().forEach(gameModeAddon -> {

Expand Down Expand Up @@ -62,13 +62,16 @@ public PlayerBorder getPlayerBorder() {
* This method loads addon configuration settings in memory.
*/
private void loadSettings() {
this.settings = new Config<>(this, Settings.class).loadConfigObject();
this.settings = config.loadConfigObject();

if (this.settings == null) {
// Disable
this.logError("Challenges settings could not load! Addon disabled.");
this.setState(State.DISABLED);
return;
}
// Save new version
this.config.saveConfigObject(settings);
}

public Settings getSettings() {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/world/bentobox/border/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ public class Settings implements ConfigObject {

@ConfigComment("")
@ConfigComment("Use barrier blocks. If false, the border is indicated by particles only.")
@ConfigEntry(path = "use-barrier-blocks")
private boolean useBarrierBlocks = true;

@ConfigComment("")
@ConfigComment("Default border behavior")
@ConfigEntry(path = "show-by-default")
private boolean showByDefault= true;

/**
* @param disabledGameModes new disabledGameModes value.
Expand Down Expand Up @@ -52,4 +58,18 @@ public boolean isUseBarrierBlocks() {
public void setUseBarrierBlocks(boolean useBarrierBlocks) {
this.useBarrierBlocks = useBarrierBlocks;
}

/**
* @return the showByDefault
*/
public boolean isShowByDefault() {
return showByDefault;
}

/**
* @param showByDefault the showByDefault to set
*/
public void setShowByDefault(boolean showByDefault) {
this.showByDefault = showByDefault;
}
}
29 changes: 24 additions & 5 deletions src/main/java/world/bentobox/border/listeners/PlayerBorder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.event.vehicle.VehicleMoveEvent;
import org.bukkit.util.Vector;

import world.bentobox.bentobox.api.metadata.MetaDataValue;
import world.bentobox.bentobox.api.user.User;
Expand All @@ -35,7 +37,8 @@ public class PlayerBorder implements Listener {
private static final BlockData BLOCK = Material.BARRIER.createBlockData();
private final Border addon;
private static final Particle PARTICLE = Particle.REDSTONE;
private static final Particle.DustOptions PARTICLE_DUST_OPTIONS = new Particle.DustOptions(Color.BLUE, 1.0F);
private static final Particle.DustOptions PARTICLE_DUST_RED = new Particle.DustOptions(Color.RED, 1.0F);
private static final Particle.DustOptions PARTICLE_DUST_BLUE = new Particle.DustOptions(Color.BLUE, 1.0F);
private static final int BARRIER_RADIUS = 5;
private final Map<UUID, Set<BarrierBlock>> barrierBlocks = new HashMap<>();

Expand Down Expand Up @@ -75,7 +78,7 @@ public void showBarrier(Player player, Island island) {
if (addon.getSettings().getDisabledGameModes().contains(island.getGameMode()))
return;

if (!User.getInstance(player).getMetaData(BORDER_STATE_META_DATA).map(MetaDataValue::asBoolean).orElse(false)) {
if (!User.getInstance(player).getMetaData(BORDER_STATE_META_DATA).map(MetaDataValue::asBoolean).orElse(addon.getSettings().isShowByDefault())) {
return;
}
// Get the locations to show
Expand Down Expand Up @@ -121,20 +124,36 @@ public void showBarrier(Player player, Island island) {

private void showPlayer(Player player, int i, int j, int k) {
// Get if on or in border
if (player.getLocation().getBlockX() == i && player.getLocation().getBlockZ() == k) {
addon.getIslands().homeTeleportAsync(player.getWorld(), player);
if (addon.getSettings().isUseBarrierBlocks()
&& player.getGameMode().equals(addon.getPlugin().getIWM().getDefaultGameMode(player.getWorld()))
&& player.getLocation().getBlockX() == i
&& player.getLocation().getBlockZ() == k) {
teleportPlayer(player);
return;
}
Location l = new Location(player.getWorld(), i, j, k);
Util.getChunkAtAsync(l).thenAccept(c -> {
User.getInstance(player).spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, i + 0.5D, j + 0.0D, k + 0.5D);
if (j < 0 || j > player.getWorld().getMaxHeight()) {
User.getInstance(player).spawnParticle(PARTICLE, PARTICLE_DUST_RED, i + 0.5D, j + 0.0D, k + 0.5D);
} else {
User.getInstance(player).spawnParticle(PARTICLE, PARTICLE_DUST_BLUE, i + 0.5D, j + 0.0D, k + 0.5D);
}
if (addon.getSettings().isUseBarrierBlocks() && l.getBlock().isEmpty() || l.getBlock().isLiquid()) {
player.sendBlockChange(l, BLOCK);
barrierBlocks.computeIfAbsent(player.getUniqueId(), u -> new HashSet<>()).add(new BarrierBlock(l, l.getBlock().getBlockData()));
}
});
}

private void teleportPlayer(Player p) {
addon.getIslands().getIslandAt(p.getLocation()).ifPresent(i -> {
Vector unitVector = i.getCenter().toVector().subtract(p.getLocation().toVector()).normalize()
.multiply(new Vector(1,0,1));
p.setVelocity(new Vector (0,0,0));
Util.teleportAsync(p, p.getLocation().toVector().add(unitVector).toLocation(p.getWorld()), TeleportCause.PLUGIN);
});
}

/**
* Hide the barrier
* @param user - user
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ disabled-gamemodes: []

# Use barrier blocks. If false, the border is indicated by particles only.
use-barrier-blocks: true

# Default border behavior
show-by-default: true

0 comments on commit d0f2421

Please sign in to comment.