Skip to content
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
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.proc>full</maven.compiler.proc>
<argLine></argLine>
<denareconomy.version>0.2.4</denareconomy.version>
<denareconomy.version>0.3.0</denareconomy.version>
<magic.version>0.2.0</magic.version>
<simplefactions.version>3.0.1</simplefactions.version>
<tlibs.version>2.0.0</tlibs.version>
<simplefactions.version>4.0.0</simplefactions.version>
<tlibs.version>2.4.0</tlibs.version>
</properties>

<dependencies>
Expand Down
55 changes: 45 additions & 10 deletions src/main/java/net/tfminecraft/dowsing/DowsingMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import net.tfminecraft.dowsing.loaders.BlockLoader;
import net.tfminecraft.dowsing.loaders.ConfigLoader;
Expand All @@ -19,10 +21,13 @@
import net.tfminecraft.dowsing.utils.Database;
import net.tfminecraft.dowsing.utils.ItemCreator;
import net.tfminecraft.dowsing.utils.TabCompletion;
import net.tfminecraft.dowsing.utils.NodePayments;
import net.tfminecraft.simplefactions.api.SimpleFactionsApi;
import net.tfminecraft.simplefactions.events.SimpleFactionsReadyEvent;
import net.tfminecraft.simplefactions.guild.income.Ledger;

@SuppressWarnings("deprecation") // Preserve legacy section-color messages used by the plugin configuration.
public class DowsingMain extends JavaPlugin{
public class DowsingMain extends JavaPlugin implements Listener {
FileConfiguration config = getConfig();

public static DowsingMain plugin;
Expand All @@ -40,6 +45,14 @@ public class DowsingMain extends JavaPlugin{
private static final NodeManager nodeManager = new NodeManager();

Database db = new Database();
private NodePayments payments;
private boolean nodesLoaded;
private boolean managersStarted;

public NodePayments payments() { return payments; }
public boolean nodesReady() {
return managersStarted && SimpleFactionsApi.ready() && !payments.blocked();
}

public static NodeManager getNodeManager() {
return nodeManager;
Expand All @@ -52,24 +65,45 @@ public void onEnable(){
createFolders();
createConfigs();
loadConfigs();
startManagers();
db.loadGuildCapacity();
db.loadNodes();
Ledger.setNodeUpkeepLookup(NodeManager::getTotalUpkeep);
NodeManager.requestNodeBenefitSync();
nodesLoaded = false;
managersStarted = false;
payments = new NodePayments(getDataFolder().toPath(), db, getLogger());
getServer().getPluginManager().registerEvents(this, plugin);
getServer().getPluginManager().registerEvents(resourceManager, plugin);
getServer().getPluginManager().registerEvents(nodeManager, plugin);
getCommand(commands.cmd1).setExecutor(commands);
getCommand(commands.cmd1).setTabCompleter(new TabCompletion());
tryStartNodes();
getServer().getScheduler().runTaskTimer(this, this::tryStartNodes, 100L, 100L);
}
@EventHandler
public void onFactionsReady(SimpleFactionsReadyEvent event) { tryStartNodes(); }

void tryStartNodes() {
if (!SimpleFactionsApi.ready()) return;
if (!nodesLoaded) {
NodeManager.nodes.clear();
NodeManager.extraCapacityByGuild.clear();
db.loadGuildCapacity();
db.loadNodes();
nodesLoaded = true;
}
if (managersStarted && !payments.blocked()) return;
if (!payments.recover()) return;
Ledger.setNodeUpkeepLookup(NodeManager::getTotalUpkeep);
NodeManager.requestNodeBenefitSync();
if (managersStarted) return;
managersStarted = true;
startManagers();
}
@Override
public void onDisable() {
Ledger.setNodeUpkeepLookup(null);
db.deleteDatabase();
for(Node n : NodeManager.nodes) {
db.saveNode(n);
if (nodesReady()) {
db.saveNodes(NodeManager.nodes);
db.saveGuildCapacity();
}
db.saveGuildCapacity();
managersStarted = false;
}
public void startManagers() {
nodeManager.start();
Expand Down Expand Up @@ -105,6 +139,7 @@ public void createConfigs() {
}
}
public void reloadConfigCommand() {
if (!nodesReady()) return;
nodeManager.cacheNodes();
ItemCreator.clearInvalidPathWarnings();
BlockLoader.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ ItemStack createGlobe(Node n) {
ItemMeta m = i.getItemMeta();
m.setDisplayName("§eBelongs to: "+g.getName());
List<String> lore = new ArrayList<String>();
lore.add("§7Leader: §f"+g.getLeader());
lore.add("§7Leader: §f"+net.tfminecraft.simplefactions.identity.Members.display(g.getLeader()));
if(NodeManager.getNodeAmount(g) < n.getCapacity()) {
lore.add("§eNodes: §a"+NodeManager.getNodeAmount(g)+"/"+n.getCapacity());
} else if(NodeManager.getNodeAmount(g) == n.getCapacity()){
Expand Down
44 changes: 33 additions & 11 deletions src/main/java/net/tfminecraft/dowsing/managers/NodeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import net.tfminecraft.dowsing.objects.NodeType;
import net.tfminecraft.dowsing.objects.ProductionMethod;
import net.tfminecraft.dowsing.utils.ItemDropper;
import net.tfminecraft.dowsing.utils.NodePayments;
import net.tfminecraft.simplefactions.api.SimpleFactionsApi;
import net.tfminecraft.dowsing.utils.NodeEngine;
import net.tfminecraft.dowsing.utils.NodeReloader;
import net.tfminecraft.dowsing.utils.NodeToggleLog;
Expand All @@ -67,6 +69,7 @@ public static void requestNodeBenefitSync() {
}

public static void syncFactionNodeBenefits() {
if (!SimpleFactionsApi.ready()) return;
Map<Faction, Double> prestigeByFaction = new HashMap<>();
Map<Faction, Double> wealthByFaction = new HashMap<>();
for(Node n : nodes) {
Expand Down Expand Up @@ -158,6 +161,7 @@ public void start() {
{
public void run()
{
if (!DowsingMain.plugin.nodesReady()) return;
validate();
for(Node n : nodes) {
try {
Expand Down Expand Up @@ -204,6 +208,7 @@ public void particleCycle(){
{
public void run()
{
if (!DowsingMain.plugin.nodesReady()) return;
for(Node n : nodes){
if(n.isClaimable()){
Location loc = n.getLoc().clone().add(0.5, 1, 0.5);
Expand All @@ -220,13 +225,15 @@ public void hourCycle(){
{
public void run()
{
if (!DowsingMain.plugin.nodesReady()) return;
for(Node n : nodes){
n.growEfficiency();
}
}
}.runTaskTimer(DowsingMain.plugin, 0L, 72000L);
}
public void validate() {
if (!SimpleFactionsApi.ready()) return;
for(int i = 0; i<nodes.size();i++) {
Node n = nodes.get(i);
if(!n.getLoc().getBlock().getType().equals(Material.BARRIER)) {
Expand All @@ -236,6 +243,7 @@ public void validate() {
}
}
public void confirmClick(Player p, Node n, ConfirmType t) {
if (!DowsingMain.plugin.nodesReady()) return;
if(t != ConfirmType.DEACTIVATE && blockPendingRefund(p, n)) return;
if(t != ConfirmType.DEACTIVATE && blockActiveChange(p, n,
t == ConfirmType.DELETE_NODE ? "delete node" : "change type")) return;
Expand Down Expand Up @@ -272,7 +280,7 @@ private boolean blockPendingRefund(Player p, Node n) {
}
private boolean canManageNode(Player p, Node n) {
if(p.hasPermission("dowsing.admin")) return true;
Guild memberGuild = FactionManager.getGuildByMember(p.getName());
Guild memberGuild = FactionManager.getGuildByMember(p);
Guild owner = n.getGuild();
if(memberGuild != null && owner != null && owner.getId().equalsIgnoreCase(memberGuild.getId())) return true;
p.sendMessage("§cCannot change another guild's node");
Expand Down Expand Up @@ -312,6 +320,7 @@ public void placeFurnitureNode(FurniturePlaceSuccessEvent e) {
}
}
private void tryCreateNode(Player p, Location loc, NodeBlock b, Cancellable e) {
if (!DowsingMain.plugin.nodesReady()) { e.setCancelled(true); return; }
if(p == null || loc == null || b == null) {
e.setCancelled(true);
return;
Expand All @@ -327,7 +336,7 @@ private void tryCreateNode(Player p, Location loc, NodeBlock b, Cancellable e) {
return;
}
}
Guild g = FactionManager.getGuildByMember(p.getName());
Guild g = FactionManager.getGuildByMember(p);
if(g == null) {
p.sendMessage("§cYou need to have a guild to use nodes!");
e.setCancelled(true);
Expand Down Expand Up @@ -371,6 +380,7 @@ private Location furnitureBlockLocation(Entity entity) {
}
@EventHandler(ignoreCancelled = true)
public void openNode(PlayerInteractEvent e) {
if (!DowsingMain.plugin.nodesReady()) return;
if(e.getClickedBlock() == null) return;
if(e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
if(getByLocation(e.getClickedBlock().getLocation()) == null) return;
Expand All @@ -384,7 +394,7 @@ public void openNode(PlayerInteractEvent e) {
InventoryManager inv = new InventoryManager();
e.setCancelled(true);
if(n.isClaimable()){
Guild g = FactionManager.getGuildByLeader(p.getName());
Guild g = FactionManager.getGuildByLeader(p);
if(g == null) {
p.sendMessage("§cMust be a guild leader to claim an unclaimed node!");
p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_NO, 1f, 1f);
Expand Down Expand Up @@ -412,6 +422,10 @@ public void openNode(PlayerInteractEvent e) {
}
@EventHandler
public void invenClick(InventoryClickEvent e) {
if (!DowsingMain.plugin.nodesReady()) {
if (currentNode.containsKey(e.getWhoClicked())) e.setCancelled(true);
return;
}
Player p = (Player) e.getWhoClicked();
if(!currentNode.containsKey(p)) return;
Node n = currentNode.get(p);
Expand All @@ -425,7 +439,7 @@ public void invenClick(InventoryClickEvent e) {
p.closeInventory();
return;
}
Guild g = FactionManager.getGuildByMember(p.getName());
Guild g = FactionManager.getGuildByMember(p);
if(!canManageNode(p, n)) return;
if(e.getSlot() != 17 && blockPendingRefund(p, n)) return;
if(e.getSlot() == 8) {
Expand Down Expand Up @@ -623,8 +637,10 @@ private void purchaseCapacity(Player p, Guild g, Node n, Inventory i) {
p.playSound(p.getLocation(), Sound.ENTITY_VILLAGER_NO, 1f, 1f);
return;
}
g.getBank().withdraw(cost);
extraCapacityByGuild.put(g.getId(), getExtraCapacity(g)+1);
if (!payments().change(n, g, "capacity", getExtraCapacity(g), getExtraCapacity(g)+1, cost)) {
p.sendMessage("§cNode payment is pending or unavailable. Try again shortly.");
return;
}
p.sendMessage("§aPurchased +1 Capacity");
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BIT, 1f, 1f);
InventoryManager inv = new InventoryManager();
Expand All @@ -646,8 +662,10 @@ public void upgradeNode(Player p, Node n, Inventory i) {
return;
}
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BIT, 1f, 1f);
g.getBank().withdraw(cost);
n.setLevel(n.getLevel()+1);
if (!payments().change(n, g, "level", n.getLevel(), n.getLevel()+1, cost)) {
p.sendMessage("§cNode payment is pending or unavailable. Try again shortly.");
return;
}
InventoryManager inv = new InventoryManager();
n.update();
requestNodeBenefitSync();
Expand All @@ -661,16 +679,20 @@ public void downgradeNode(Player p, Node n, Inventory i) {
}
Level lvl = n.getCurrentType().getLevels().get(n.getLevel()-1);
Double refund = lvl.getCost()*Cache.refundPercentage;
if(n.getGuild() != null && n.getGuild().getBank() != null) {
n.getGuild().getBank().deposit(refund);
if(n.getGuild() == null || n.getGuild().getBank() == null ||
!payments().change(n, n.getGuild(), "level", n.getLevel(), n.getLevel()-1, -refund)) {
p.sendMessage("§cNode payment is pending or unavailable. Try again shortly.");
return;
}
p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_BIT, 1f, 1f);
n.setLevel(n.getLevel()-1);
InventoryManager inv = new InventoryManager();
n.update();
requestNodeBenefitSync();
inv.updateNodeView(p, n, i);
}
private NodePayments payments() {
return DowsingMain.plugin.payments();
}
public void cacheNodes() {
cached.clear();
for(Node n : nodes) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/net/tfminecraft/dowsing/objects/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import net.tfminecraft.dowsing.utils.ItemDropper;
import net.tfminecraft.dowsing.utils.NodeEngine;
import net.tfminecraft.simplefactions.guild.Guild;
import net.tfminecraft.simplefactions.api.SimpleFactionsApi;
import net.tfminecraft.simplefactions.identity.Members;
import net.tfminecraft.simplefactions.managers.FactionManager;
import net.tfminecraft.simplefactions.utils.Formatter;
import net.tfminecraft.simplefactions.utils.Permissions;
Expand Down Expand Up @@ -241,7 +243,7 @@ public void setCostIncrease(Double costIncrease) {
this.costIncrease = costIncrease;
}
public boolean isClaimable() {
return resolveGuild() == null;
return SimpleFactionsApi.ready() && resolveGuild() == null;
}

//Efficiency
Expand All @@ -250,6 +252,7 @@ public double getEfficiency() {
}

Guild resolveGuild() {
if (!SimpleFactionsApi.ready()) return guild;
if(guild == null) {
return null;
}
Expand All @@ -264,7 +267,7 @@ Guild resolveGuild() {

public boolean canHold(Guild g){
if(g == null) return false;
Player p = Bukkit.getPlayerExact(g.getLeader());
Player p = Members.player(g.getLeader());
if(p != null && p.isOnline() && Permissions.isAdmin(p)) return true;
if(g.getMembers().size() < Cache.minMembersForNode) return false;
if(NodeManager.getNodeAmount(g)-NodeManager.getNodeCapacity(g) > 0) return false;
Expand Down Expand Up @@ -357,11 +360,12 @@ Integer getNaturalYieldFromChunk(NodeBlock b, Location l) throws NumberFormatExc
}

public void check() {
if (!SimpleFactionsApi.ready()) return;
if(isClaimable()) return;
Guild g = resolveGuild();
if(g == null) return;
if(canHold(g)) return;
Player p = Bukkit.getPlayer(g.getLeader());
Player p = Members.player(g.getLeader());
if(p != null && p.isOnline()) {
p.sendMessage("§cYou lost control of the "+block.getResource()+" Node §c!");
p.closeInventory();
Expand Down
Loading