This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break Commons's functions down into modules
- Loading branch information
BGM
committed
Apr 5, 2020
1 parent
8625250
commit 376c846
Showing
8 changed files
with
189 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Commons/src/main/java/cl/bgmp/commons/Modules/JoinToolsModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cl.bgmp.commons.Modules; | ||
|
||
import cl.bgmp.commons.Commons; | ||
import cl.bgmp.commons.Config; | ||
import cl.bgmp.utilsbukkit.Chat; | ||
import cl.bgmp.utilsbukkit.Items.Items; | ||
import org.bukkit.Material; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class JoinToolsModule extends Module { | ||
public static final String id = "join-module"; | ||
|
||
private static List<ItemStack> onJoinTools = new ArrayList<ItemStack>() {{ | ||
add(Items.titledItemStackWithLore(Material.COMPASS, Chat.colourify("&9&lTeleport Tool&r"), new String[]{Chat.colourify("&7Click to teleport!&r")})); | ||
add(Items.titledItemStack(Material.RABBIT_FOOT, Chat.colourify("&5&lEdit Wand"))); | ||
}}; | ||
|
||
public JoinToolsModule() { | ||
super(id); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.LOWEST) | ||
public void onPlayerJoin(final PlayerJoinEvent event) { | ||
final Player player = event.getPlayer(); | ||
player.getInventory().clear(); | ||
|
||
final Module module = Commons.get().getModule(NavigatorModule.id); | ||
if (module != null) { | ||
final NavigatorModule navigatorModule = (NavigatorModule) module; | ||
if (navigatorModule.isEnabled()) player.getInventory().setItem(4, navigatorModule.getNavigator().getItem()); | ||
} | ||
|
||
if (!player.hasPermission("commons.tools")) return; | ||
player.getInventory().setItem(0, onJoinTools.get(0)); | ||
player.getInventory().setItem(1, onJoinTools.get(1)); | ||
} | ||
|
||
@Override | ||
public void load() { | ||
this.enabled = Config.Tools.areEnabled(); | ||
if (enabled) Commons.get().registerEvents(this); | ||
} | ||
|
||
@Override | ||
public void unload() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cl.bgmp.commons.Modules; | ||
|
||
import org.bukkit.event.Listener; | ||
|
||
public abstract class Module implements Listener { | ||
protected String id; | ||
protected boolean enabled; | ||
|
||
public Module(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public abstract void load(); | ||
|
||
public abstract void unload(); | ||
} |
10 changes: 10 additions & 0 deletions
10
Commons/src/main/java/cl/bgmp/commons/Modules/ModuleManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package cl.bgmp.commons.Modules; | ||
|
||
public interface ModuleManager { | ||
|
||
void registerModules(Module... modules); | ||
|
||
void loadModules(); | ||
|
||
Module getModule(String id); | ||
} |
54 changes: 54 additions & 0 deletions
54
Commons/src/main/java/cl/bgmp/commons/Modules/NavigatorModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cl.bgmp.commons.Modules; | ||
|
||
import cl.bgmp.commons.Commons; | ||
import cl.bgmp.commons.Config; | ||
import cl.bgmp.commons.Navigator.Navigator; | ||
import cl.bgmp.commons.Navigator.NavigatorGUI; | ||
import org.bukkit.Material; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.player.PlayerInteractEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.SkullMeta; | ||
|
||
public class NavigatorModule extends Module { | ||
public static final String id = "navigator-module"; | ||
private Navigator navigator = new Navigator(); | ||
|
||
public NavigatorModule() { | ||
super(id); | ||
} | ||
|
||
public Navigator getNavigator() { | ||
return navigator; | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerInteract(PlayerInteractEvent event) { | ||
final ItemStack itemInHand = event.getItem(); | ||
if (itemInHand == null || itemInHand.getType() == Material.AIR) return; | ||
|
||
final ItemStack navigatorItem = navigator.getItem(); | ||
boolean itemInHandIsNavigator = false; | ||
|
||
if (navigatorItem.getItemMeta() instanceof SkullMeta) { | ||
if (!itemInHand.hasItemMeta() | ||
|| !itemInHand.getItemMeta().hasLore() | ||
|| !navigatorItem.getItemMeta().hasLore() | ||
) return; | ||
|
||
if (itemInHand.getLore().equals(navigatorItem.getLore())) itemInHandIsNavigator = true; | ||
} else if (itemInHand.equals(navigator)) itemInHandIsNavigator = true; | ||
|
||
if (itemInHandIsNavigator) event.getPlayer().openInventory(new NavigatorGUI().getInventory()); | ||
} | ||
|
||
@Override | ||
public void load() { | ||
this.enabled = Config.Navigator.isEnabled(); | ||
if (enabled) Commons.get().registerEvents(this); | ||
} | ||
|
||
@Override | ||
public void unload() { | ||
} | ||
} |
58 changes: 4 additions & 54 deletions
58
Commons/src/main/java/cl/bgmp/commons/Navigator/Navigator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,20 @@ | ||
package cl.bgmp.commons.Navigator; | ||
|
||
import cl.bgmp.commons.Commons; | ||
import cl.bgmp.commons.Config; | ||
import cl.bgmp.utilsbukkit.Chat; | ||
import cl.bgmp.utilsbukkit.Items.Items; | ||
import cl.bgmp.utilsbukkit.Items.PlayerHeads; | ||
import org.bukkit.Material; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerInteractEvent; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.meta.SkullMeta; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
|
||
/** The server navigator players are given when joining */ | ||
public class Navigator implements Listener { | ||
public class Navigator { | ||
private String title = Chat.colourify("&9&lNavigator&r"); | ||
private String[] lore = new String[] {Chat.colourify("&7Right-Click to pick a Server!&r")}; | ||
|
||
// Note: if navigator happens to be a player head, it must contain a lore for validation matters! | ||
private ItemStack navigator = PlayerHeads.titledPlayerHeadWithLore("Skyron_Varn", title, lore); | ||
|
||
// Note: if navigator's item happens to be a player head, it must contain a lore for validation matters! | ||
public Navigator() { | ||
if (Config.Navigator.isEnabled()) { | ||
Commons.get().registerEvents(this); | ||
} | ||
} | ||
|
||
// TODO: Move this out of here and come up with an idea to modularise it | ||
private static List<ItemStack> onJoinItems = new ArrayList<ItemStack>() {{ | ||
add(Items.titledItemStackWithLore(Material.COMPASS, Chat.colourify("&9&lTeleport Tool&r"), new String[]{Chat.colourify("&7Click to teleport!&r")})); | ||
add(Items.titledItemStack(Material.RABBIT_FOOT, Chat.colourify("&5&lEdit Wand"))); | ||
}}; | ||
|
||
@EventHandler | ||
public void onPlayerJoin(PlayerJoinEvent event) { | ||
final Player player = event.getPlayer(); | ||
player.getInventory().clear(); | ||
player.getInventory().setItem(4, navigator); | ||
|
||
if (player.hasPermission("commons.items.compass")) player.getInventory().addItem(onJoinItems.get(0)); | ||
if (player.hasPermission("commons.items.rabbit_foot")) player.getInventory().addItem(onJoinItems.get(1)); | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerInteract(PlayerInteractEvent event) { | ||
final ItemStack itemInHand = event.getItem(); | ||
if (itemInHand == null || itemInHand.getType() == Material.AIR) return; | ||
|
||
boolean itemInHandIsNavigator = false; | ||
|
||
if (navigator.getItemMeta() instanceof SkullMeta) { | ||
if (!itemInHand.hasItemMeta() | ||
|| !itemInHand.getItemMeta().hasLore() | ||
|| !navigator.getItemMeta().hasLore() | ||
) return; | ||
|
||
if (itemInHand.getLore().equals(navigator.getLore())) itemInHandIsNavigator = true; | ||
} else if (itemInHand.equals(navigator)) itemInHandIsNavigator = true; | ||
|
||
if (itemInHandIsNavigator) event.getPlayer().openInventory(new NavigatorGUI().getInventory()); | ||
public ItemStack getItem() { | ||
return navigator; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
onjoin-tools: true | ||
navigator: | ||
enabled: true | ||
title: "&b&lAvailable Servers" | ||
|