Skip to content

Commit

Permalink
Configurable Worlds
Browse files Browse the repository at this point in the history
  • Loading branch information
YayaDuck authored and yayaduckd committed Nov 29, 2022
1 parent 5d85f51 commit 41067d1
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.yayaduck.paper</groupId>
<artifactId>perworldadvancements</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.0</version>
<packaging>jar</packaging>

<name>PerWorldAdvancements</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import java.util.ArrayList;

public class AdvancementListener implements Listener {

private final PerWorldAdvancements plugin;
Expand All @@ -14,23 +16,43 @@ public class AdvancementListener implements Listener {

/**
* Cancels all advancements and recipes when the player is not in survival mode.
*
* This method is called when a player unlocks an advancement or recipe.
*
* @pre {@code event != null}
* @post {@code event.isCancelled() == true}
*/
@EventHandler(priority = org.bukkit.event.EventPriority.HIGH)
void onAdvancement(PlayerAdvancementCriterionGrantEvent event) {
if (event.getPlayer().getWorld().equals(Bukkit.getWorld("Creative"))) {
event.setCancelled(true);
if (plugin.getConfig().getBoolean("enableDebug")) {
Bukkit.getLogger().info(
"PerWorldAdvancements: Advancement "
+ event.getAdvancement().getKey().getKey() +
" was cancelled for " + event.getPlayer().getName() +
" in world " + event.getPlayer().getWorld().getName() + "!");
ArrayList<String> worlds = (ArrayList<String>) plugin.getConfig().getStringList("worlds");
String playerWorld = event.getPlayer().getWorld().getName();

for (String world : worlds) {
if (playerWorld.equals(world)) {
cancelAdvancement(event);
}
}
}

/**
* Cancels the advancement provided.
* @pre {@code event != null}
* @post {@code event.isCancelled() == true}
* @throws NullPointerException if provided event is null.
* @param event The advancement to cancel.
*/
private void cancelAdvancement(PlayerAdvancementCriterionGrantEvent event) throws NullPointerException {
if (event == null) {
throw new NullPointerException("event cannot be null");
}

event.setCancelled(true);

if (plugin.getConfig().getBoolean("enableDebug")) {
Bukkit.getLogger().info(
"PerWorldAdvancements: Advancement "
+ event.getAdvancement().getKey().getKey() +
" was cancelled for " + event.getPlayer().getName() +
" in world " + event.getPlayer().getWorld().getName() + "!");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package com.yayaduck.paper.perworldadvancements;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.ArrayList;

public final class PerWorldAdvancements extends JavaPlugin {
@Override
public void onEnable() {
getLogger().info("\"PerWorldAdvancements: Enabled! 🦆");
setDefaults();
getServer().getPluginManager().registerEvents(new AdvancementListener(this), this);

this.getConfig().addDefault("enableDebug", false);
this.saveDefaultConfig();
getLogger().info("PerWorldAdvancements: Enabled! 🦆");
getLogger().info("PerWorldAdvancements: Worlds configured: "
+ getConfig().getStringList("worlds"));
}

@Override
public void onDisable() {
getLogger().info("PerWorldAdvancements: Byebye! 🦃");
}

private void setDefaults() {
FileConfiguration config = this.getConfig();

config.addDefault("enableDebug", false);

ArrayList<String> worlds = new ArrayList<>();
worlds.add("exampleworld1");
worlds.add("exampleworld2");
worlds.add("exampleworld2_nether");
config.addDefault("worlds", worlds);

saveDefaultConfig();
}
}
9 changes: 8 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
enableDebug: false
# If set to true, the plugin will log information about every cancelled advancement to the console.
enableDebug: false

# The worlds where achievements will be disabled.
worlds:
- "exampleworld1"
- "exampleworld2"
- "exampleworld2_nether"

0 comments on commit 41067d1

Please sign in to comment.