Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vault support #253

Open
wants to merge 1 commit into
base: indev
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@
<id>CodeMC</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>

<!-- VaultAPI -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>

</repositories>

<dependencies>
Expand All @@ -142,5 +149,11 @@
<version>1.8</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
13 changes: 11 additions & 2 deletions src/main/java/fr/moribus/imageonmap/ImageOnMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import fr.moribus.imageonmap.commands.maptool.NewCommand;
import fr.moribus.imageonmap.commands.maptool.RenameCommand;
import fr.moribus.imageonmap.commands.maptool.UpdateCommand;
import fr.moribus.imageonmap.economy.EconomyComponent;
import fr.moribus.imageonmap.image.ImageIOExecutor;
import fr.moribus.imageonmap.image.ImageRendererExecutor;
import fr.moribus.imageonmap.image.MapInitEvent;
Expand Down Expand Up @@ -113,15 +114,23 @@ public void onEnable() {

saveDefaultConfig();
commandWorker = loadComponent(CommandWorkers.class);
loadComponents(I18n.class, Gui.class, Commands.class, PluginConfiguration.class, ImageIOExecutor.class,
ImageRendererExecutor.class);
loadComponents(
I18n.class,
Gui.class,
Commands.class,
PluginConfiguration.class,
ImageIOExecutor.class,
ImageRendererExecutor.class,
EconomyComponent.class
);

//Init all the things !
I18n.setPrimaryLocale(PluginConfiguration.LANG.get());

MapManager.init();
MapInitEvent.init();
MapItemManager.init();
EconomyComponent.init();


Commands.register(
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/fr/moribus/imageonmap/PluginConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public final class PluginConfiguration extends Configuration {
public static ConfigurationList<String> IMAGES_HOSTNAMES_WHITELIST =
list("images-hostnames-whitelist", String.class);

}
public static ConfigurationItem<Boolean> ENABLE_ECONOMY = item("enable-economy", false);
public static ConfigurationItem<Boolean> SCALE_MAP_COST = item("scale-map-cost", true);
public static ConfigurationItem<Integer> COST_PER_MAP = item("cost-per-map", 100);
public static ConfigurationItem<Boolean> SCALE_COPY_COST = item("scale-copy-cost", false);
public static ConfigurationItem<Integer> COST_PER_COPY = item("cost-per-copy", 50);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.Permissions;
import fr.moribus.imageonmap.commands.IoMCommand;
import fr.moribus.imageonmap.economy.EconomyNotEnabledException;
import fr.moribus.imageonmap.economy.InsufficientFundsException;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import fr.zcraft.quartzlib.components.commands.CommandException;
Expand Down Expand Up @@ -96,10 +98,19 @@ protected void run() throws CommandException {
return;
}

if (map.give(sender)) {
info(I.t("The requested map was too big to fit in your inventory."));
info(I.t("Use '/maptool getremaining' to get the remaining maps."));
try {
if (map.give(sender)) {
info(I.t("The requested map was too big to fit in your inventory."));
info(I.t("Use '/maptool getremaining' to get the remaining maps."));
}
} catch (EconomyNotEnabledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InsufficientFundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import fr.moribus.imageonmap.Permissions;
import fr.moribus.imageonmap.commands.IoMCommand;
import fr.moribus.imageonmap.economy.EconomyNotEnabledException;
import fr.moribus.imageonmap.economy.InsufficientFundsException;
import fr.moribus.imageonmap.ui.MapItemManager;
import fr.zcraft.quartzlib.components.commands.CommandException;
import fr.zcraft.quartzlib.components.commands.CommandInfo;
Expand All @@ -56,14 +58,25 @@ protected void run() throws CommandException {
return;
}

int givenMaps = MapItemManager.giveCache(player);

if (givenMaps == 0) {
error(I.t("Your inventory is full! Make some space before requesting the remaining maps."));
} else {
info(I.tn("There is {0} map remaining.", "There are {0} maps remaining.",
MapItemManager.getCacheSize(player)));
try {
int givenMaps = MapItemManager.giveCache(player);
if (givenMaps == 0) {
error(I.t("Your inventory is full! Make some space before requesting the remaining maps."));
} else {
info(I.tn(
"There is {0} map remaining.", "There are {0} maps remaining.",
MapItemManager.getCacheSize(player)
));
}
} catch (EconomyNotEnabledException exception) {
// TODO Auto-generated catch block
exception.printStackTrace();
} catch (InsufficientFundsException exception) {
// TODO Auto-generated catch block
exception.printStackTrace();
}


}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import fr.moribus.imageonmap.Permissions;
import fr.moribus.imageonmap.commands.IoMCommand;
import fr.moribus.imageonmap.economy.EconomyNotEnabledException;
import fr.moribus.imageonmap.economy.InsufficientFundsException;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import fr.zcraft.quartzlib.components.commands.CommandException;
Expand Down Expand Up @@ -111,11 +113,21 @@ protected void run() throws CommandException {
}

retrieveUUID(playerName, uuid2 -> {
if (Bukkit.getPlayer((uuid2)) != null && Bukkit.getPlayer((uuid2)).isOnline()
&& map.give(Bukkit.getPlayer(uuid2))) {
info(I.t("The requested map was too big to fit in your inventory."));
info(I.t("Use '/maptool getremaining' to get the remaining maps."));

try {
if (Bukkit.getPlayer((uuid2)) != null && Bukkit.getPlayer((uuid2)).isOnline()
&& map.give(Bukkit.getPlayer(uuid2))) {
info(I.t("The requested map was too big to fit in your inventory."));
info(I.t("Use '/maptool getremaining' to get the remaining maps."));
}
} catch (EconomyNotEnabledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InsufficientFundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
package fr.moribus.imageonmap.commands.maptool;

import fr.moribus.imageonmap.Permissions;
import fr.moribus.imageonmap.PluginConfiguration;
import fr.moribus.imageonmap.commands.IoMCommand;
import fr.moribus.imageonmap.economy.EconomyNotEnabledException;
import fr.moribus.imageonmap.economy.InsufficientFundsException;
import fr.moribus.imageonmap.image.ImageRendererExecutor;
import fr.moribus.imageonmap.image.ImageUtils;
import fr.moribus.imageonmap.map.ImageMap;
Expand Down Expand Up @@ -162,11 +165,20 @@ public void finished(ImageMap result) {
MessageSender
.sendActionBarMessage(player, ChatColor.DARK_GREEN + I.t("Rendering finished!"));

if (result.give(player)
&& (result instanceof PosterMap && !((PosterMap) result).hasColumnData())) {
info(I.t("The rendered map was too big to fit in your inventory."));
info(I.t("Use '/maptool getremaining' to get the remaining maps."));
try {
if (result.give(player)
&& (result instanceof PosterMap && !((PosterMap) result).hasColumnData())) {
info(I.t("The rendered map was too big to fit in your inventory."));
info(I.t("Use '/maptool getremaining' to get the remaining maps."));
}
} catch (EconomyNotEnabledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InsufficientFundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/fr/moribus/imageonmap/economy/EconomyComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fr.moribus.imageonmap.economy;

import fr.moribus.imageonmap.PluginConfiguration;
import fr.zcraft.quartzlib.core.QuartzComponent;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;

public final class EconomyComponent extends QuartzComponent {

private static Economy economy = null;

public static void init() {
System.out.println("HELLO!");
if (PluginConfiguration.ENABLE_ECONOMY.get()
&& Bukkit.getPluginManager().getPlugin("Vault") != null) {
RegisteredServiceProvider<Economy> rsp = Bukkit.getServicesManager().getRegistration(Economy.class);
if (rsp != null) {
economy = rsp.getProvider();
}
}
}

public static Economy getEconomy() throws EconomyNotEnabledException {

if (economy != null) {
return economy;
} else {
throw new EconomyNotEnabledException();
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.moribus.imageonmap.economy;

public class EconomyNotEnabledException extends Exception {

public EconomyNotEnabledException() {
super("The economy is disabled");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.moribus.imageonmap.economy;

import org.bukkit.OfflinePlayer;

public class InsufficientFundsException extends Exception {

private final OfflinePlayer player;
private final double cost;

public InsufficientFundsException(OfflinePlayer player, double cost) {
super("Insufficient funds");
this.player = player;
this.cost = cost;
}

public OfflinePlayer getPlayer() {
return player;
}

public double getCost() {
return cost;
}

}
40 changes: 27 additions & 13 deletions src/main/java/fr/moribus/imageonmap/gui/MapListGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@

import fr.moribus.imageonmap.Permissions;
import fr.moribus.imageonmap.PluginConfiguration;
import fr.moribus.imageonmap.economy.EconomyNotEnabledException;
import fr.moribus.imageonmap.economy.InsufficientFundsException;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import fr.moribus.imageonmap.map.PosterMap;
Expand Down Expand Up @@ -143,25 +145,37 @@ protected void onClose() {

@Override
protected ItemStack getPickedUpItem(ImageMap map) {
if (!Permissions.GET.grantedTo(getPlayer())) {
return null;
}

if (map instanceof SingleMap) {
return MapItemManager.createMapItem(map.getMapsIDs()[0], map.getName(), false, true);
} else if (map instanceof PosterMap) {
PosterMap poster = (PosterMap) map;
try {

if (poster.hasColumnData()) {
return SplatterMapManager.makeSplatterMap((PosterMap) map);
if (!Permissions.GET.grantedTo(getPlayer())) {
return null;
}

MapItemManager.giveParts(getPlayer(), poster);
return null;
}
if (map instanceof SingleMap) {
return MapItemManager.createMapItem(map.getMapsIDs()[0], map.getName(), false, true);
} else if (map instanceof PosterMap) {
PosterMap poster = (PosterMap) map;

if (poster.hasColumnData()) {
return SplatterMapManager.makeSplatterMap((PosterMap) map);
}

MapItemManager.give(getPlayer(), map);
MapItemManager.giveParts(getPlayer(), poster);
return null;
}

MapItemManager.give(getPlayer(), map);

} catch (EconomyNotEnabledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InsufficientFundsException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;

}

@Override
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/fr/moribus/imageonmap/map/ImageMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
package fr.moribus.imageonmap.map;

import fr.moribus.imageonmap.ImageOnMap;
import fr.moribus.imageonmap.economy.EconomyNotEnabledException;
import fr.moribus.imageonmap.economy.InsufficientFundsException;
import fr.moribus.imageonmap.ui.MapItemManager;
import fr.zcraft.quartzlib.components.i18n.I;
import java.io.File;
Expand Down Expand Up @@ -168,8 +170,20 @@ public boolean managesMap(ItemStack item) {

//

public boolean give(Player player) {
return MapItemManager.give(player, this);
public boolean give(Player player, boolean ignoreCost) {
try {
return MapItemManager.give(player, this, ignoreCost);
} catch (EconomyNotEnabledException | InsufficientFundsException exception) {
if (ignoreCost) {
//TODO it shouldn't get to this state
exception.printStackTrace();
}
return false;
}
}

public boolean give(Player player) throws EconomyNotEnabledException, InsufficientFundsException {
return give(player, false);
}

protected abstract void postSerialize(Map<String, Object> map);
Expand Down
Loading