Skip to content

Commit

Permalink
Added configurable list of safe blocks that players can teleport to (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Mar 17, 2022
1 parent dedb586 commit e34e978
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 34 deletions.
Expand Up @@ -8,6 +8,7 @@
import org.bukkit.configuration.ConfigurationSection;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -130,6 +131,11 @@ public interface SettingsManager {
*/
List<String> getInteractables();

/**
* Get all the safe blocks.
*/
Collection<Key> getSafeBlocks();

/**
* Whether visitors should take damage on islands or not.
* Config path: visitors-damage
Expand Down
@@ -1,5 +1,6 @@
package com.bgsoftware.superiorskyblock.config;

import com.bgsoftware.common.config.CommentedConfiguration;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.objects.Pair;
import com.bgsoftware.superiorskyblock.handler.HandlerLoadException;
Expand All @@ -16,15 +17,18 @@
import com.bgsoftware.superiorskyblock.utils.items.TemplateItem;
import com.bgsoftware.superiorskyblock.values.BlockValuesHandler;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -106,6 +110,7 @@ public final class SettingsContainer {
public final boolean voidTeleportMembers;
public final boolean voidTeleportVisitors;
public final List<String> interactables;
public final KeySet safeBlocks;
public final boolean visitorsDamage;
public final boolean coopDamage;
public final int disbandCount;
Expand Down Expand Up @@ -323,6 +328,7 @@ else if (sections.length == 3)
voidTeleportMembers = config.getBoolean("void-teleport.members", true);
voidTeleportVisitors = config.getBoolean("void-teleport.visitors", true);
interactables = loadInteractables(plugin);
safeBlocks = loadSafeBlocks(plugin);
visitorsDamage = config.getBoolean("visitors-damage", false);
coopDamage = config.getBoolean("coop-damage", true);
disbandCount = config.getInt("disband-count", 5);
Expand Down Expand Up @@ -486,6 +492,37 @@ private List<String> loadInteractables(SuperiorSkyblockPlugin plugin) {
return cfg.getStringList("interactables");
}

private KeySet loadSafeBlocks(SuperiorSkyblockPlugin plugin) {
File file = new File(plugin.getDataFolder(), "safe_blocks.yml");

if (!file.exists())
FileUtils.saveResource("safe_blocks.yml");

CommentedConfiguration cfg = CommentedConfiguration.loadConfiguration(file);

List<String> safeBlocks = cfg.getStringList("safe-blocks");

if (!safeBlocks.isEmpty())
return new KeySet(safeBlocks);

SuperiorSkyblockPlugin.log("&c[safe_blocks.yml] There are no valid safe blocks! Generating default ones...");

List<String> safeBlocksDefaults = Arrays.stream(Material.values())
.filter(Material::isSolid)
.map(Material::name)
.sorted()
.collect(Collectors.toList());

try {
cfg.set("safe-blocks", safeBlocksDefaults);
cfg.save(file);
} catch (IOException error) {
PluginDebugger.debug(error);
}

return new KeySet(safeBlocksDefaults);
}

private void loadGenerator(List<String> lines, int index) {
defaultGenerator[index] = new KeyMap<>();
for (String line : lines) {
Expand Down
Expand Up @@ -26,6 +26,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -183,6 +184,11 @@ public List<String> getInteractables() {
return this.container.interactables;
}

@Override
public Collection<Key> getSafeBlocks() {
return this.container.safeBlocks;
}

@Override
public boolean isVisitorsDamage() {
return this.container.visitorsDamage;
Expand Down
Expand Up @@ -2,21 +2,17 @@

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.api.player.algorithm.PlayerTeleportAlgorithm;
import com.bgsoftware.superiorskyblock.threads.Executor;
import com.bgsoftware.superiorskyblock.utils.LocationUtils;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;
import com.bgsoftware.superiorskyblock.utils.legacy.Materials;
import com.bgsoftware.superiorskyblock.utils.teleport.TeleportUtils;
import com.bgsoftware.superiorskyblock.world.chunks.ChunkPosition;
import com.bgsoftware.superiorskyblock.world.chunks.ChunksProvider;
import com.google.common.base.Preconditions;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -118,6 +114,8 @@ public CompletableFuture<Boolean> teleport(Player player, Island island, World.E
true, true, null)
.stream().map(future -> future.thenApply(Chunk::getChunkSnapshot)).collect(Collectors.toList());

World islandsWorld = plugin.getGrid().getIslandsWorld(island, environment);

Executor.createTask().runAsync(v -> {
List<Location> safeLocations = new ArrayList<>();

Expand All @@ -135,34 +133,19 @@ public CompletableFuture<Boolean> teleport(Player player, Island island, World.E
if (LocationUtils.isChunkEmpty(null, chunkSnapshot))
continue;

World world = Bukkit.getWorld(chunkSnapshot.getWorldName());
int worldBuildLimit = world.getMaxHeight() - 1;
int worldMinLimit = plugin.getNMSWorld().getMinHeight(world);
int worldBuildLimit = islandsWorld.getMaxHeight() - 1;
int worldMinLimit = plugin.getNMSWorld().getMinHeight(islandsWorld);

for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
int y = Math.min(chunkSnapshot.getHighestBlockYAt(x, z), worldBuildLimit);
int y = chunkSnapshot.getHighestBlockYAt(x, z);

if (y < worldMinLimit)
if (y < worldMinLimit || y + 2 > worldBuildLimit)
continue;

Key blockKey = plugin.getNMSWorld().getBlockKey(chunkSnapshot, x, y, z);
Key belowKey = plugin.getNMSWorld().getBlockKey(chunkSnapshot, x,
y <= worldMinLimit ? worldMinLimit : y - 1, z);

Material blockType;
Material belowType;

try {
blockType = Material.valueOf(blockKey.getGlobalKey());
belowType = Material.valueOf(belowKey.getGlobalKey());
} catch (IllegalArgumentException ex) {
continue;
}

if (blockType.isSolid() || belowType.isSolid()) {
safeLocations.add(new Location(Bukkit.getWorld(chunkSnapshot.getWorldName()),
chunkSnapshot.getX() * 16 + x, y, chunkSnapshot.getZ() * 16 + z));
if (LocationUtils.isSafeBlock(chunkSnapshot, x, y, z)) {
safeLocations.add(new Location(islandsWorld,
chunkSnapshot.getX() * 16 + x, y + 1, chunkSnapshot.getZ() * 16 + z));
}
}
}
Expand All @@ -187,9 +170,7 @@ public CompletableFuture<Boolean> teleport(Player player, Island island, World.E

private void adjustAndTeleportPlayerToLocation(Player player, Island island, Location location, float yaw,
float pitch, Consumer<Boolean> result) {
double yAdjustment = Materials.isSlab(location.getBlock().getType()) ? -0.5 : 0;

Location homeLocation = location.add(0.5, yAdjustment, 0.5);
Location homeLocation = location.add(0.5, 0, 0.5);
homeLocation.setYaw(yaw);
homeLocation.setPitch(pitch);

Expand Down
Expand Up @@ -2,16 +2,20 @@

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.key.Key;
import com.bgsoftware.superiorskyblock.utils.debug.PluginDebugger;
import com.bgsoftware.superiorskyblock.world.chunks.ChunksTracker;
import com.bgsoftware.superiorskyblock.utils.locations.SmartLocation;
import com.bgsoftware.superiorskyblock.world.chunks.ChunksTracker;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;

public final class LocationUtils {

private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin();

private LocationUtils() {

}
Expand Down Expand Up @@ -42,10 +46,29 @@ public static String getLocation(Location location) {
}

public static boolean isSafeBlock(Block block) {
Block underBlock = block.getRelative(BlockFace.DOWN);
Block upperBlock = block.getRelative(BlockFace.UP);
return !upperBlock.getType().isOccluding() && !block.getType().isOccluding() &&
(underBlock.getType().isOccluding() || underBlock.getRelative(BlockFace.DOWN).getType().isOccluding());
Block feetBlock = block.getRelative(BlockFace.UP);
Block headBlock = feetBlock.getRelative(BlockFace.UP);

if (feetBlock.getType().isSolid() || headBlock.getType().isSolid())
return false;

return plugin.getSettings().getSafeBlocks().contains(Key.of(block));
}

public static boolean isSafeBlock(ChunkSnapshot chunkSnapshot, int x, int y, int z) {
Key feetBlockKey = plugin.getNMSWorld().getBlockKey(chunkSnapshot, x, y + 1, z);
Key headBlockKey = plugin.getNMSWorld().getBlockKey(chunkSnapshot, x, y + 2, z);

try {
if (Material.valueOf(feetBlockKey.getGlobalKey()).isSolid() ||
Material.valueOf(headBlockKey.getGlobalKey()).isSolid())
return false;
} catch (IllegalArgumentException error) {
return false;
}

Key standingBlockKey = plugin.getNMSWorld().getBlockKey(chunkSnapshot, x, y, z);
return plugin.getSettings().getSafeBlocks().contains(standingBlockKey);
}

public static boolean isChunkEmpty(Island island, ChunkSnapshot chunkSnapshot) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/safe_blocks.yml
@@ -0,0 +1,10 @@
######################################################
## ##
## SuperiorSkyblock 2 ##
## Developed by Ome_R ##
## ##
######################################################

# A list of safe blocks.
# Players can only get teleported (when teleporting to islands) on blocks from this list.
safe-blocks: [ ]

0 comments on commit e34e978

Please sign in to comment.