Skip to content

Commit

Permalink
Change world difficulty in edit menu
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Meaney <[email protected]>
  • Loading branch information
thomasmny committed Mar 30, 2022
1 parent fa597d9 commit b5ec849
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void loadWorlds(WorldManager worldManager) {

worldManager.getBuildWorlds().forEach(world -> {
String worldName = world.getName();
worldManager.generateBukkitWorld(worldName, world.getType(), world.getChunkGenerator());
worldManager.generateBukkitWorld(worldName, world.getType(), world.getDifficulty(), world.getChunkGenerator());

if (world.getMaterial() == XMaterial.PLAYER_HEAD) {
plugin.getSkullCache().cacheSkull(worldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public Inventory getInventory(Player player, BuildWorld buildWorld) {
addVisibilityItem(inventory, buildWorld, player);
addSettingsItem(inventory, 33, XMaterial.TRIPWIRE_HOOK, buildWorld.isBlockInteractions(), plugin.getString("worldeditor_blockinteractions_item"), plugin.getStringList("worldeditor_blockinteractions_lore"));
inventoryManager.addItemStack(inventory, 38, XMaterial.FILLED_MAP, plugin.getString("worldeditor_gamerules_item"), plugin.getStringList("worldeditor_gamerules_lore"));
inventoryManager.addItemStack(inventory, 39, inventoryManager.getStatusItem(buildWorld.getStatus()), plugin.getString("worldeditor_status_item"), getStatusLore(buildWorld));
addDifficultyItem(inventory, buildWorld);
inventoryManager.addItemStack(inventory, 40, inventoryManager.getStatusItem(buildWorld.getStatus()), plugin.getString("worldeditor_status_item"), getStatusLore(buildWorld));
inventoryManager.addItemStack(inventory, 41, XMaterial.ANVIL, plugin.getString("worldeditor_project_item"), getProjectLore(buildWorld));
inventoryManager.addItemStack(inventory, 42, XMaterial.PAPER, plugin.getString("worldeditor_permission_item"), getPermissionLore(buildWorld));

Expand Down Expand Up @@ -121,9 +122,8 @@ private void addTimeItem(Inventory inventory, BuildWorld buildWorld) {

XMaterial xMaterial = XMaterial.WHITE_STAINED_GLASS;
String value = plugin.getString("worldeditor_time_lore_unknown");
BuildWorld.Time time = getWorldTime(bukkitWorld);

switch (time) {
switch (getWorldTime(bukkitWorld)) {
case SUNRISE:
xMaterial = XMaterial.ORANGE_STAINED_GLASS;
value = plugin.getString("worldeditor_time_lore_sunrise");
Expand Down Expand Up @@ -191,6 +191,30 @@ private void addVisibilityItem(Inventory inventory, BuildWorld buildWorld, Playe
inventoryManager.addItemStack(inventory, slot, xMaterial, displayName, lore);
}

private void addDifficultyItem(Inventory inventory, BuildWorld buildWorld) {
XMaterial xMaterial;

switch (buildWorld.getDifficulty()) {
case EASY:
xMaterial = XMaterial.GOLDEN_HELMET;
break;
case NORMAL:
xMaterial = XMaterial.IRON_HELMET;
break;
case HARD:
xMaterial = XMaterial.DIAMOND_HELMET;
break;
default:
xMaterial = XMaterial.LEATHER_HELMET;
break;
}

ArrayList<String> lore = new ArrayList<>();
plugin.getStringList("worldeditor_difficulty_lore").forEach(line -> lore.add(line.replace("%difficulty%", buildWorld.getDifficultyName())));

inventoryManager.addItemStack(inventory, 39, xMaterial, plugin.getString("worldeditor_difficulty_item"), lore);
}

private List<String> getStatusLore(BuildWorld buildWorld) {
List<String> lore = new ArrayList<>();
for (String line : plugin.getStringList("worldeditor_status_lore")) {
Expand Down Expand Up @@ -286,6 +310,10 @@ public void onInventoryClick(InventoryClickEvent event) {
plugin.getGameRuleInventory().openInventory(player, buildWorld);
return;
case 39:
buildWorld.cycleDifficulty();
buildWorld.getWorld().setDifficulty(buildWorld.getDifficulty());
break;
case 40:
XSound.ENTITY_CHICKEN_EGG.play(player);
plugin.getStatusInventory().openInventory(player);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.eintosti.buildsystem.util.external.PlayerChatInput;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Difficulty;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
Expand Down Expand Up @@ -308,7 +309,7 @@ public boolean createCustomWorld(Player player, String worldName, boolean privat
player.sendMessage(plugin.getString("worlds_world_creation_started")
.replace("%world%", buildWorld.getName())
.replace("%type%", buildWorld.getTypeName()));
generateBukkitWorld(worldName, buildWorld.getType(), chunkGenerator);
generateBukkitWorld(worldName, buildWorld.getType(), buildWorld.getDifficulty(), chunkGenerator);
player.sendMessage(plugin.getString("worlds_creation_finished"));
});
return true;
Expand Down Expand Up @@ -358,7 +359,7 @@ private boolean createTemplateWorld(Player player, String worldName, String temp
*/
private void finishPreparationsAndGenerate(BuildWorld buildWorld) {
WorldType worldType = buildWorld.getType();
World bukkitWorld = generateBukkitWorld(buildWorld.getName(), worldType);
World bukkitWorld = generateBukkitWorld(buildWorld.getName(), worldType, buildWorld.getDifficulty(), buildWorld.getChunkGenerator());

switch (worldType) {
case VOID:
Expand All @@ -381,10 +382,11 @@ private void finishPreparationsAndGenerate(BuildWorld buildWorld) {
*
* @param worldName The name of the world
* @param worldType The world type
* @param difficulty The world's difficulty
* @param chunkGenerators Custom chunk generator to be used, if any
* @return The world object
*/
public World generateBukkitWorld(String worldName, WorldType worldType, ChunkGenerator... chunkGenerators) {
public World generateBukkitWorld(String worldName, WorldType worldType, Difficulty difficulty, ChunkGenerator... chunkGenerators) {
WorldCreator worldCreator = new WorldCreator(worldName);
org.bukkit.WorldType bukkitWorldType;

Expand Down Expand Up @@ -434,7 +436,7 @@ public World generateBukkitWorld(String worldName, WorldType worldType, ChunkGen

World bukkitWorld = Bukkit.createWorld(worldCreator);
if (bukkitWorld != null) {
bukkitWorld.setDifficulty(configValues.getWorldDifficulty());
bukkitWorld.setDifficulty(difficulty);
bukkitWorld.setTime(configValues.getNoonTime());
bukkitWorld.getWorldBorder().setSize(configValues.getWorldBorderSize());
bukkitWorld.setKeepSpawnInMemory(configValues.isTeleportAfterCreation());
Expand Down Expand Up @@ -516,7 +518,7 @@ public void importWorld(Player player, String worldName, Generator generator, St
false
);
buildWorlds.add(buildWorld);
generateBukkitWorld(worldName, generator.getWorldType(), chunkGenerator);
generateBukkitWorld(worldName, generator.getWorldType(), buildWorld.getDifficulty(), chunkGenerator);
player.sendMessage(plugin.getString("worlds_import_finished"));

if (configValues.isTeleportAfterCreation()) {
Expand Down Expand Up @@ -552,8 +554,9 @@ public void run() {
}

long creation = FileUtils.getDirectoryCreation(new File(Bukkit.getWorldContainer(), worldName));
buildWorlds.add(new BuildWorld(plugin, worldName, "-", null, WorldType.IMPORTED, creation, false));
generateBukkitWorld(worldName, WorldType.VOID);
BuildWorld buildWorld = new BuildWorld(plugin, worldName, "-", null, WorldType.IMPORTED, creation, false);
buildWorlds.add(buildWorld);
generateBukkitWorld(worldName, WorldType.VOID, buildWorld.getDifficulty());
player.sendMessage(plugin.getString("worlds_importall_world_imported").replace("%world%", worldName));

if (!(worldsImported.get() < worlds)) {
Expand Down Expand Up @@ -709,7 +712,7 @@ public void renameWorld(Player player, BuildWorld buildWorld, String newName) {
FileUtils.deleteDirectory(oldWorldFile);

buildWorld.setName(newName);
World newWorld = generateBukkitWorld(buildWorld.getName(), buildWorld.getType(), buildWorld.getChunkGenerator());
World newWorld = generateBukkitWorld(buildWorld.getName(), buildWorld.getType(), buildWorld.getDifficulty(), buildWorld.getChunkGenerator());
Location spawnLocation = oldWorld.getSpawnLocation();
spawnLocation.setWorld(newWorld);

Expand Down Expand Up @@ -850,15 +853,11 @@ public BuildWorld loadWorld(String worldName) {
boolean blockPlacement = !configuration.isBoolean("worlds." + worldName + ".block-placement") || configuration.getBoolean("worlds." + worldName + ".block-placement");
boolean blockInteractions = !configuration.isBoolean("worlds." + worldName + ".block-interactions") || configuration.getBoolean("worlds." + worldName + ".block-interactions");
boolean buildersEnabled = configuration.isBoolean("worlds." + worldName + ".builders-enabled") && configuration.getBoolean("worlds." + worldName + ".builders-enabled");
Difficulty difficulty = Difficulty.valueOf(configuration.getString("worlds." + worldName + ".difficulty", "PEACEFUL").toUpperCase());
List<Builder> builders = parseBuilders(configuration, worldName);
String chunkGeneratorString = configuration.getString("worlds." + worldName + ".chunk-generator");
ChunkGenerator chunkGenerator = parseChunkGenerator(configuration, worldName);

if (worldType == WorldType.PRIVATE) {
privateWorld = true;
worldType = WorldType.FLAT;
}

BuildWorld buildWorld = new BuildWorld(
plugin,
worldName,
Expand All @@ -879,6 +878,7 @@ public BuildWorld loadWorld(String worldName) {
blockPlacement,
blockInteractions,
buildersEnabled,
difficulty,
builders,
chunkGenerator,
chunkGeneratorString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.eintosti.buildsystem.util.exception.UnexpectedEnumValueException;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Difficulty;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
Expand Down Expand Up @@ -70,6 +71,7 @@ public class BuildWorld implements ConfigurationSerializable {
private boolean blockPlacement;
private boolean blockInteractions;
private boolean buildersEnabled;
private Difficulty difficulty;

private long seconds;
private boolean loaded;
Expand Down Expand Up @@ -107,6 +109,7 @@ public BuildWorld(
this.blockPlacement = configValues.isWorldBlockPlacement();
this.blockInteractions = configValues.isWorldBlockInteractions();
this.buildersEnabled = isPrivate();
this.difficulty = configValues.getWorldDifficulty();
this.chunkGeneratorName = (chunkGeneratorName != null && chunkGeneratorName.length > 0) ? chunkGeneratorName[0] : null;

InventoryManager inventoryManager = plugin.getInventoryManager();
Expand Down Expand Up @@ -169,6 +172,7 @@ public BuildWorld(
boolean blockPlacement,
boolean blockInteractions,
boolean buildersEnabled,
Difficulty difficulty,
List<Builder> builders,
ChunkGenerator chunkGenerator,
String chunkGeneratorName
Expand All @@ -194,13 +198,23 @@ public BuildWorld(
this.blockPlacement = blockPlacement;
this.blockInteractions = blockInteractions;
this.buildersEnabled = buildersEnabled;
this.difficulty = difficulty;
this.builders = builders;
this.chunkGenerator = chunkGenerator;
this.chunkGeneratorName = chunkGeneratorName;

manageUnload();
}

/**
* Get the world linked to this object.
*
* @return The bukkit world
*/
public World getWorld() {
return Bukkit.getWorld(name);
}

/**
* Get the name of the world.
*
Expand Down Expand Up @@ -378,7 +392,7 @@ public void setStatus(WorldStatus worldStatus) {
* Get the display name of a {@link WorldStatus}.
*
* @return the status's display name
* @see BuildWorld#getStatus() ()
* @see BuildWorld#getStatus()
*/
public String getStatusName() {
switch (worldStatus) {
Expand Down Expand Up @@ -608,6 +622,56 @@ public void setBlockInteractions(boolean blockInteractions) {
this.blockInteractions = blockInteractions;
}

/**
* Gets the world's difficulty
*
* @return the difficulty
*/
public Difficulty getDifficulty() {
return difficulty;
}

/**
* Get the display name of a {@link Difficulty}.
*
* @return the difficulty's display name
* @see BuildWorld#getDifficulty()
*/
public String getDifficultyName() {
switch (difficulty) {
case PEACEFUL:
return plugin.getString("difficulty_peaceful");
case EASY:
return plugin.getString("difficulty_easy");
case NORMAL:
return plugin.getString("difficulty_normal");
case HARD:
return plugin.getString("difficulty_hard");
default:
return "-";
}
}

/**
* Cycles to the next {@link Difficulty}.
*/
public void cycleDifficulty() {
switch (difficulty) {
case PEACEFUL:
this.difficulty = Difficulty.EASY;
break;
case EASY:
this.difficulty = Difficulty.NORMAL;
break;
case NORMAL:
this.difficulty = Difficulty.HARD;
break;
case HARD:
this.difficulty = Difficulty.PEACEFUL;
break;
}
}

/**
* If enabled, only {@link Builder}s can break and place blocks in the world.
*
Expand Down Expand Up @@ -868,7 +932,7 @@ public void load() {
}

plugin.getLogger().info("*** Loading world \"" + name + "\" ***");
plugin.getWorldManager().generateBukkitWorld(name, worldType, chunkGenerator);
plugin.getWorldManager().generateBukkitWorld(name, worldType, difficulty, chunkGenerator);
this.loaded = true;

resetUnloadTask();
Expand All @@ -893,6 +957,7 @@ public void load() {
world.put("block-breaking", isBlockBreaking());
world.put("block-placement", isBlockPlacement());
world.put("block-interactions", isBlockInteractions());
world.put("difficulty", getDifficulty().toString());
world.put("builders-enabled", isBuilders());
world.put("builders", saveBuilders());
if (customSpawn != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ public void createMessageFile() {
setMessage(sb, config, "type_template", "Template");
setMessage(sb, config, "type_private", "Private");
addLine(sb, "");
addLine(sb, "# Worlds Status");
addLine(sb, "# World Status");
setMessage(sb, config, "status_title", "&8Status &7» &3%world%");
setMessage(sb, config, "status_not_started", "&cNot Started");
setMessage(sb, config, "status_in_progress", "&6In Progress");
Expand All @@ -485,6 +485,12 @@ public void createMessageFile() {
setMessage(sb, config, "status_archive", "&9Archive");
setMessage(sb, config, "status_hidden", "&fHidden");
addLine(sb, "");
addLine(sb, "# World Difficulty");
setMessage(sb, config, "difficulty_peaceful", "&fPeaceful");
setMessage(sb, config, "difficulty_easy", "&aEasy");
setMessage(sb, config, "difficulty_normal", "&6Normal");
setMessage(sb, config, "difficulty_hard", "&cHard");
addLine(sb, "");
addLine(sb, "# Delete World");
setMessage(sb, config, "delete_title", "&3» &8Delete World");
setMessage(sb, config, "delete_world_name", "&e%world%");
Expand Down Expand Up @@ -549,19 +555,22 @@ public void createMessageFile() {
setList(sb, config, "worldeditor_visibility_lore_private", Arrays.asList("&7&oChange the world's visibility", "", "&7&nCurrently&7: &bPrivate"));
addLine(sb, "");
setMessage(sb, config, "worldeditor_mobai_item", "&bMob AI");
setList(sb, config, "worldeditor_mobai_lore", Arrays.asList("&7&oToggle where or not mobs", "&7&ohave an AI."));
setList(sb, config, "worldeditor_mobai_lore", Arrays.asList("&7&oToggle whether mobs have an AI."));
addLine(sb, "");
setMessage(sb, config, "worldeditor_blockinteractions_item", "&bBlock Interactions");
setList(sb, config, "worldeditor_blockinteractions_lore", Arrays.asList("&7&oToggle whether or not interactions", "&7&owith blocks are cancelled."));
setList(sb, config, "worldeditor_blockinteractions_lore", Arrays.asList("&7&oToggle whether interactions", "&7&owith blocks are cancelled."));
addLine(sb, "");
setMessage(sb, config, "worldeditor_difficulty_item", "&bDifficulty");
setList(sb, config, "worldeditor_difficulty_lore", Arrays.asList("&7&oChange the world's difficulty.", "", "&7&nCurrently&7: %difficulty%"));
addLine(sb, "");
setMessage(sb, config, "worldeditor_status_item", "&bStatus");
setList(sb, config, "worldeditor_status_lore", Arrays.asList("&7&oChange the world's current", "&7&ostatus.", "", "&7&nCurrently&7: %status%"));
setList(sb, config, "worldeditor_status_lore", Arrays.asList("&7&oChange the world's status.", "", "&7&nCurrently&7: %status%"));
addLine(sb, "");
setMessage(sb, config, "worldeditor_project_item", "&bProject");
setList(sb, config, "worldeditor_project_lore", Arrays.asList("&7&oChange the world's current", "&7&oproject.", "", "&7&nCurrently&7: &b%project%"));
setList(sb, config, "worldeditor_project_lore", Arrays.asList("&7&oChange the world's project.", "", "&7&nCurrently&7: &b%project%"));
addLine(sb, "");
setMessage(sb, config, "worldeditor_permission_item", "&bPermission");
setList(sb, config, "worldeditor_permission_lore", Arrays.asList("&7&oChange the world's current", "&7&opermission.", "", "&7&nCurrently&7: &b%permission%"));
setList(sb, config, "worldeditor_permission_lore", Arrays.asList("&7&oChange the world's permission.", "", "&7&nCurrently&7: &b%permission%"));
addLine(sb, "");
addLine(sb, "# Settings");
setMessage(sb, config, "settings_title", "&3» &8Settings");
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group=com.eintosti
version=2.18.6-dev
version=2.18.6
org.gradle.daemon=true
org.gradle.caching=true
org.gradle.parallel=true
Expand Down

0 comments on commit b5ec849

Please sign in to comment.