Skip to content

Commit

Permalink
Add clone command, add setsize command, add "available_for_rent" plac…
Browse files Browse the repository at this point in the history
…eholder, add towny support
  • Loading branch information
Bestem0r committed Jul 10, 2022
1 parent 92e4993 commit 3679d31
Show file tree
Hide file tree
Showing 11 changed files with 375 additions and 64 deletions.
19 changes: 16 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>me.bestemor.villagermarket</groupId>
<artifactId>VillagerMarket</artifactId>
<version>1.10.5-SNAPSHOT</version>
<version>1.10.6-SNAPSHOT</version>

<repositories>
<repository>
Expand All @@ -23,6 +23,11 @@
<url>https://jitpack.io</url>
</repository>

<repository>
<id>glaremasters repo</id>
<url>https://repo.glaremasters.me/repository/towny/</url>
</repository>

<repository>
<id>sk89q-repo</id>
<url>https://maven.enginehub.org/repo/</url>
Expand Down Expand Up @@ -80,6 +85,14 @@
</exclusions>
</dependency>

<!-- Towny -->
<dependency>
<groupId>com.palmergames.bukkit.towny</groupId>
<artifactId>towny</artifactId>
<version>0.98.2.0</version>
<scope>provided</scope>
</dependency>

<!-- PAPI -->
<dependency>
<groupId>me.clip</groupId>
Expand Down Expand Up @@ -122,8 +135,8 @@
<configuration>
<relocations>
<relocation>
<pattern>org.bstats.bukkit</pattern>
<shadedPattern>me.bestem0r.villagermarket.utils.bstats</shadedPattern>
<pattern>org.bstats</pattern>
<shadedPattern>net.bestemor.villagermarket.utils.bstats</shadedPattern>
</relocation>
<relocation>
<pattern>net.bestemor.core</pattern>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.bestemor.villagermarket;

import net.bestemor.core.config.ConfigManager;
import net.bestemor.villagermarket.shop.AdminShop;
import net.bestemor.villagermarket.shop.PlayerShop;
import net.bestemor.villagermarket.shop.VillagerShop;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
Expand Down Expand Up @@ -57,6 +58,12 @@ public String onPlaceholderRequest(Player player, @NotNull String params) {
if (params.equals("owned_shops")) {
return String.valueOf(plugin.getShopManager().getOwnedShops(player).size());
}
if (params.equals("available_for_rent")) {
return String.valueOf(plugin.getShopManager().getShops().stream()
.filter(s -> s instanceof PlayerShop)
.map(s -> (PlayerShop) s)
.filter(s -> !s.hasOwner()).count());
}
if (params.equals("max_shops")) {
int maxShops = plugin.getShopManager().getMaxShops(player);

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/bestemor/villagermarket/VMPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void onEnable() {
this.playerListener = new PlayerListener(this);
registerEvents();

//Bukkit.getLogger().warning("[VillagerMarket] §cYou are running a §aBETA 1.10.5-#4 of VillagerMarket! Please expect and report all bugs in my discord server");
//Bukkit.getLogger().warning("[VillagerMarket] §cYou are running a §aBETA 1.10.6-#2 of VillagerMarket! Please expect and report all bugs in my discord server");

Bukkit.getScheduler().runTaskLater(this, () -> {
if (Bukkit.getPluginManager().getPlugin("VillagerBank") != null) {
Expand Down Expand Up @@ -121,6 +121,8 @@ private void setupCommands() {
.addSubCommand("getid", new GetIDCommand(this))
.addSubCommand("expiredstorage", new ExpiredStorageCommand(this))
.addSubCommand("regen", new RegenCommand(this))
.addSubCommand("clone", new CloneCommand(this))
.addSubCommand("setsize", new SetSizeCommand(this))
.permissionPrefix("villagermarket.command")
.build();

Expand Down Expand Up @@ -149,7 +151,6 @@ private void registerEvents() {
PluginManager pluginManager = Bukkit.getPluginManager();
pluginManager.registerEvents(new EntityListener(this), this);
pluginManager.registerEvents(playerListener, this);
//pluginManager.registerEvents(new ChunkLoad(this), this);
pluginManager.registerEvents(menuListener, this);
pluginManager.registerEvents(chatListener, this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package net.bestemor.villagermarket.command.subcommand;

import jdk.nashorn.internal.runtime.regexp.joni.Config;
import net.bestemor.core.command.ISubCommand;
import net.bestemor.core.config.ConfigManager;
import net.bestemor.villagermarket.VMPlugin;
import net.bestemor.villagermarket.listener.PlayerListener;
import net.bestemor.villagermarket.shop.ShopManager;
import net.bestemor.villagermarket.shop.VillagerShop;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.CommandSender;
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.PlayerInteractEntityEvent;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class CloneCommand implements ISubCommand {

private final VMPlugin plugin;
private final CloneListener listener;

public CloneCommand(VMPlugin plugin) {
this.plugin = plugin;
this.listener = new CloneListener();
Bukkit.getPluginManager().registerEvents(this.listener, plugin);
}

@Override
public List<String> getCompletion(String[] strings) {
return new ArrayList<>();
}

@Override
public void run(CommandSender sender, String[] strings) {
if (sender instanceof Player) {
Player player = (Player) sender;

if (listener.players.contains(player.getUniqueId())) {
return;
}

plugin.getPlayerEvents().addCancelledPlayer(player.getUniqueId());
listener.players.add(player.getUniqueId());
player.sendMessage(ConfigManager.getMessage("messages.clone_shop"));
}
}

@Override
public String getDescription() {
return "Clone Villager Shop";
}

@Override
public String getUsage() {
return "";
}

@Override
public boolean requirePermission() {
return true;
}

private class CloneListener implements Listener {
private final List<UUID> players = new ArrayList<>();

@EventHandler(priority = EventPriority.LOW)
public void onInteract(PlayerInteractEntityEvent event) {

if (!players.contains(event.getPlayer().getUniqueId())) { return; }
event.setCancelled(true);

VillagerShop shop = plugin.getShopManager().getShop(event.getRightClicked().getUniqueId());
Player player = event.getPlayer();
if (shop != null) {
plugin.getShopManager().cloneShop(shop, player.getLocation());
player.sendMessage(ConfigManager.getMessage("messages.shop_cloned"));
} else {
player.sendMessage(ConfigManager.getMessage("messages.no_villager_shop"));
}
plugin.getPlayerEvents().removeCancelledPlayer(player.getUniqueId());
players.remove(player.getUniqueId());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void run(CommandSender sender, String[] args) {
Entity entity = plugin.getShopManager().spawnShop(player.getLocation(), type);
player.sendMessage(ConfigManager.getMessage("messages.id").replace("%id%", entity.getUniqueId().toString()));
if (Bukkit.getEntity(entity.getUniqueId()) != null) {
plugin.getShopManager().createShopConfig(plugin, entity.getUniqueId(), storageSize, shopSize, cost, type.toUpperCase(Locale.ROOT), duration);
plugin.getShopManager().createShopConfig(entity.getUniqueId(), storageSize, shopSize, cost, type.toUpperCase(Locale.ROOT), duration);
} else {
Bukkit.getLogger().severe(ChatColor.RED + "Unable to spawn Villager! Does WorldGuard deny mobs pawn?");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package net.bestemor.villagermarket.command.subcommand;

import net.bestemor.core.command.ISubCommand;
import net.bestemor.core.config.ConfigManager;
import net.bestemor.villagermarket.VMPlugin;
import net.bestemor.villagermarket.shop.VillagerShop;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
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.PlayerInteractEntityEvent;

import java.util.*;

public class SetSizeCommand implements ISubCommand {

private final VMPlugin plugin;
private final List<String> sizes = Arrays.asList("infinite", "1", "2", "3", "4", "5", "6");
private final SetSizeListener listener;

public SetSizeCommand(VMPlugin plugin) {
this.plugin = plugin;
this.listener = new SetSizeListener();
Bukkit.getPluginManager().registerEvents(this.listener, plugin);
}

@Override
public List<String> getCompletion(String[] args) {
switch (args.length) {
case 2:
return Arrays.asList("storage", "shopfront");
case 3:
return sizes;
}
return new ArrayList<>();
}

@Override
public void run(CommandSender sender, String[] args) {
if (sender instanceof Player) {
Player player = (Player) sender;

if (listener.listeningFor.containsKey(player.getUniqueId())) {
return;
}

if (args.length != 3) {
player.sendMessage(ChatColor.RED + "Incorrect usage: Please specify storage or shopfront and a size.");
return;
}
if (!args[1].equals("storage") && !args[1].equals("shopfront")) {
player.sendMessage(ChatColor.RED + "Incorrect usage: Please specify storage or shopfront.");
return;
}
if (!sizes.contains(args[2])) {
player.sendMessage(ChatColor.RED + "Incorrect usage: Please specify a correct size.");
return;
}

plugin.getPlayerEvents().addCancelledPlayer(player.getUniqueId());
SetAction action = new SetAction(args[1], args[2].equals("infinite") ? 0 : Integer.parseInt(args[2]));
listener.listeningFor.put(player.getUniqueId(), action);
player.sendMessage(ConfigManager.getMessage("messages.set_size"));
}
}

@Override
public String getDescription() {
return "Set shop size or storage size";
}

@Override
public String getUsage() {
return "<shop|storage> <size>";
}

private class SetSizeListener implements Listener {
private final Map<UUID, SetAction> listeningFor = new HashMap<>();

@EventHandler(priority = EventPriority.LOW)
public void onInteract(PlayerInteractEntityEvent event) {

if (!listeningFor.containsKey(event.getPlayer().getUniqueId())) { return; }
event.setCancelled(true);

VillagerShop shop = plugin.getShopManager().getShop(event.getRightClicked().getUniqueId());
Player player = event.getPlayer();
if (shop != null) {
SetAction action = listeningFor.get(player.getUniqueId());

shop.closeAllMenus();
if (action.action.equals("storage")) {
shop.setStorageSize(action.size);
} else {
shop.setShopfrontSize(action.size);
}
shop.save();
plugin.getShopManager().loadShop(shop.getFile());
player.sendMessage(ConfigManager.getMessage("messages.size_set").replace("%size%", action.size == 0 ? "infinite" : String.valueOf(action.size)));

} else {
player.sendMessage(ConfigManager.getMessage("messages.no_villager_shop"));
}
plugin.getPlayerEvents().removeCancelledPlayer(player.getUniqueId());
listeningFor.remove(player.getUniqueId());
}
}

private static class SetAction {
private final String action;
private final int size;

public SetAction(String action, int size) {
this.action = action;
this.size = size;
}
}
}
Loading

0 comments on commit 3679d31

Please sign in to comment.