Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to block movement on starting a oneblock. #367 #368

Merged
merged 1 commit into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/main/java/world/bentobox/aoneblock/AOneBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Objects;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
Expand All @@ -24,6 +25,7 @@
import world.bentobox.aoneblock.listeners.ItemsAdderListener;
import world.bentobox.aoneblock.listeners.JoinLeaveListener;
import world.bentobox.aoneblock.listeners.NoBlockHandler;
import world.bentobox.aoneblock.listeners.StartSafetyListener;
import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlockCreator;
import world.bentobox.aoneblock.oneblocks.OneBlocksManager;
import world.bentobox.aoneblock.oneblocks.customblock.ItemsAdderCustomBlock;
Expand All @@ -32,6 +34,9 @@
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.configuration.Config;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.Flag.Mode;
import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.database.objects.Island;

/**
Expand All @@ -53,6 +58,14 @@ public class AOneBlock extends GameModeAddon {
private OneBlocksManager oneBlockManager;
private AOneBlockPlaceholders phManager;
private HoloListener holoListener;

// Flag
public final Flag START_SAFETY = new Flag.Builder("START_SAFETY", Material.BAMBOO_BLOCK)
.mode(Mode.BASIC)
.type(Type.WORLD_SETTING)
.listener(new StartSafetyListener(this))
.defaultSetting(false)
.build();

@Override
public void onLoad() {
Expand All @@ -73,10 +86,13 @@ public void onLoad() {
// Register commands
playerCommand = new PlayerCommand(this);
adminCommand = new AdminCommand(this);
// Register flag with BentoBox
// Register protection flag with BentoBox
getPlugin().getFlagsManager().registerFlag(this, START_SAFETY);
}
}

private boolean loadSettings() {
private boolean loadSettings() {
// Load settings again to get worlds
settings = configObject.loadConfigObject();
if (settings == null) {
Expand Down Expand Up @@ -305,4 +321,18 @@ public HoloListener getHoloListener() {
public boolean hasItemsAdder() {
return hasItemsAdder;
}

/**
* Set the addon's world. Used only for testing.
* @param world world
*/
public void setIslandWorld(World world) {
this.islandWorld = world;

}

public void setSettings(Settings settings) {
this.settings = settings;
}

}
19 changes: 19 additions & 0 deletions src/main/java/world/bentobox/aoneblock/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public class Settings implements WorldSettings {
@ConfigEntry(path = "world.hologram-duration")
private int hologramDuration = 10;

@ConfigComment("Duration in seonds that players cannot move when they start a new one block.")
@ConfigComment("Used only if the Starting Safety world setting is active.")
@ConfigEntry(path = "world.starting-safety-duration")
private int startingSafetyDuration = 10;

@ConfigComment("Clear blocks when spawning mobs.")
@ConfigComment("Mobs break blocks when they spawn is to prevent players from building a box around the magic block,")
@ConfigComment("having the mob spawn, and then die by suffocation, i.e., it's a cheat prevention.")
Expand Down Expand Up @@ -2061,4 +2066,18 @@ public boolean isClearBlocks() {
public void setClearBlocks(boolean clearBlocks) {
this.clearBlocks = clearBlocks;
}

/**
* @return the startingSafetyDuration
*/
public int getStartingSafetyDuration() {
return startingSafetyDuration;
}

/**
* @param startingSafetyDuration the startingSafetyDuration to set
*/
public void setStartingSafetyDuration(int startingSafetyDuration) {
this.startingSafetyDuration = startingSafetyDuration;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package world.bentobox.aoneblock.listeners;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;

import world.bentobox.aoneblock.AOneBlock;
import world.bentobox.bentobox.api.events.island.IslandCreatedEvent;
import world.bentobox.bentobox.api.events.island.IslandResetEvent;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;

/**
* Listener to provide protection for players in the first minute of their island. Prevents movement.
*/
public class StartSafetyListener implements Listener {

private final AOneBlock addon;
private final Map<UUID, Long> newIslands = new HashMap<>();

public StartSafetyListener(AOneBlock addon) {
super();
this.addon = addon;
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onNewIsland(IslandCreatedEvent e) {
store(e.getIsland().getWorld(), e.getPlayerUUID());
}

private void store(World world, UUID playerUUID) {
if (addon.inWorld(world) && addon.START_SAFETY.isSetForWorld(world) && !newIslands.containsKey(playerUUID)) {
long time = addon.getSettings().getStartingSafetyDuration();
if (time < 0) {
time = 10; // 10 seconds
}
newIslands.put(playerUUID, System.currentTimeMillis() + (time * 1000));
Bukkit.getScheduler().runTaskLater(addon.getPlugin(), () -> {
newIslands.remove(playerUUID);
User.getInstance(playerUUID).sendMessage("protection.flags.START_SAFETY.free-to-move");
}, time);
}

}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onResetIsland(IslandResetEvent e) {
store(e.getIsland().getWorld(), e.getPlayerUUID());
}

@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerMove(PlayerMoveEvent e) {
if (addon.inWorld(e.getPlayer().getWorld()) && newIslands.containsKey(e.getPlayer().getUniqueId())
&& e.getTo() != null && !e.getPlayer().isSneaking()
&& (e.getFrom().getX() != e.getTo().getX() || e.getFrom().getZ() != e.getTo().getZ())) {
// Do not allow x or z movement
e.setTo(new Location(e.getFrom().getWorld(), e.getFrom().getX(), e.getTo().getY(), e.getFrom().getZ(),
e.getTo().getYaw(), e.getTo().getPitch()));
String waitTime = String
.valueOf((int) ((newIslands.get(e.getPlayer().getUniqueId()) - System.currentTimeMillis()) / 1000));
User.getInstance(e.getPlayer()).notify(addon.START_SAFETY.getHintReference(), TextVariables.NUMBER,
waitTime);
}
}

}
11 changes: 11 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
# the one at http://yaml-online-parser.appspot.com #
###########################################################################################

protection:
flags:
START_SAFETY:
name: Starting Safety
description: |
&b Prevents new players
&b from moving for 1 minute
&b so they don't fall off.
hint: "&c Movement blocked for safety for [number] more seconds!"
free-to-move: "&a You are free to move. Be careful!"

aoneblock:
commands:
admin:
Expand Down
Loading
Loading