Skip to content

Commit

Permalink
shops works!
Browse files Browse the repository at this point in the history
  • Loading branch information
Raffa50 committed Jan 11, 2020
1 parent 6deb415 commit 18ef86e
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 28 deletions.
62 changes: 49 additions & 13 deletions src/main/java/aldrigos/mc/villagershops/CommandCompleter.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,72 @@
package aldrigos.mc.villagershops;

import org.bukkit.Material;
import org.bukkit.command.*;
import org.bukkit.entity.Villager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class CommandCompleter implements TabCompleter {
private static final List<String>
subCommands = Arrays.asList("new", "delete", "?", "help", "list", "goto", "add", "remove");

private final ShopsManager manager;
private final Logger log;

public CommandCompleter(ShopsManager manager){
public CommandCompleter(ShopsManager manager, Logger logger){
this.manager = manager;
this.log = logger;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String s, String[] args) {
if(!command.getName().toLowerCase().equals("vshop"))
return null;
private List<String> getMaterials(String startsWith){
return Arrays.stream(Material.values())
.map(m -> m.toString().toLowerCase())
.filter(s -> s.startsWith(startsWith.toLowerCase()))
.collect(Collectors.toList());
}

private List<String> getShopNames(String startsWith){
return manager.getShops().stream()
.map(s -> s.name)
.filter(s -> s.startsWith(startsWith))
.collect(Collectors.toList());
}

if(args.length == 0)
return Arrays.asList("new", "delete", "?", "help", "list", "goto", "add", "remove");
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String l, String[] args) {
if(args.length < 1)
return subCommands;

switch(args[0].toLowerCase()){
case "delete":
case "goto":
var ret = new ArrayList<String>();
for(var shop: manager.getShops())
ret.add(shop.name);
return ret;
return getShopNames(args.length > 1 ? args[1] : "");

case "new":
if(args.length >= 4)
return Arrays.stream(Villager.Profession.values()).map(s -> s.toString()).collect(Collectors.toList());
return null;

}
case "remove":
if(args.length <= 2)
return getShopNames(args.length > 1 ? args[1] : "");
return null;

return null;
case "add": // /vshop add <id> <qty> <request> <qty> <result>
if(args.length == 2)
return manager.getShops().stream().map(s -> s.name).collect(Collectors.toList());
if(args.length == 4)
return getMaterials(args[3]);
if(args.length == 6)
return getMaterials(args[5]);
return null;

default:
return args.length < 2 ? subCommands : null;
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/aldrigos/mc/villagershops/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ public enum Messages {
PARAM_MISS(ChatColor.RED+"[VS]Missing parameter. Usage: %s"),
PARAM_INVALID(ChatColor.RED+"[VS]Invalid parameter %s"),
PLAYERCMD("[VS]This is a player command!"),
SHOP_EXIST("[VS]There is already a shop with id %s"),
SHOP_NOT_EXIST("[VS]Shop %s does not exist");
SHOP_DELETED(ChatColor.GREEN+"[VS]Shop deleted"),
SHOP_EXIST(ChatColor.RED+"[VS]There is already a shop with id %s"),
SHOP_NOT_EXIST(ChatColor.RED+"[VS]Shop %s does not exist"),
TRADE_ADDED(ChatColor.GREEN+"[VS]Trade added to shop %s"),
TRADE_REMOVED(ChatColor.GREEN+"[VS]Trade removed from shop %s");

private final String msg;

Expand Down
11 changes: 6 additions & 5 deletions src/main/java/aldrigos/mc/villagershops/ShopsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@
import org.bukkit.Location;
import org.bukkit.entity.*;
import org.bukkit.inventory.Merchant;
import org.bukkit.inventory.MerchantRecipe;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

public class ShopsManager {
private Map<UUID, Shop> shops = new HashMap<>();
private transient VillagerShopsPlugin p;

public Iterable<Shop> getShops(){ return shops.values(); }
public Collection<Shop> getShops(){ return shops.values(); }

public UUID createShop(String id, String name, Location pos, Villager.Profession type){
return createShop(new Shop(id, name, pos, type));
Expand Down Expand Up @@ -95,7 +94,9 @@ public void addTrade(String id, Trade trade) {

shop.getTrades().add(trade);
var vshop = (Merchant) p.getServer().getEntity(entityId);
vshop.getRecipes().add(trade.getRecipe());
var newTrades = new ArrayList<MerchantRecipe>(vshop.getRecipes());
newTrades.add(trade.getRecipe());
vshop.setRecipes(newTrades);
}

public void removeTrade(String id, int num){
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/aldrigos/mc/villagershops/VillagerShopsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public void onEnable(){
try{
load();
}catch (Exception ex){
getServer().getLogger().throwing(VillagerShopsPlugin.class.getName(), "onEnable", ex);
getServer().getLogger().severe("[VS]load error");
getLogger().throwing(VillagerShopsPlugin.class.getName(), "onEnable", ex);
getLogger().severe("[VS]load error");
setEnabled(false);
return;
}
Expand All @@ -55,16 +55,16 @@ public void onEnable(){

var vshopCmd = getCommand("vshop");
vshopCmd.setExecutor(new VShopCommands(this));
vshopCmd.setTabCompleter(new CommandCompleter(manager));
vshopCmd.setTabCompleter(new CommandCompleter(manager, getLogger()));
}

@Override
public void onDisable(){
try {
save();
} catch (Exception ex) {
getServer().getLogger().throwing(VillagerShopsPlugin.class.getName(), "onEnable", ex);
getServer().getLogger().severe("[VS]save error");
getLogger().throwing(VillagerShopsPlugin.class.getName(), "onEnable", ex);
getLogger().severe("[VS]save error");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean execute(CommandSender sender, LinkedList<String> args) {
short requestQty, resultQty;

try{
requestQty = Short.valueOf(args.get(1));
requestQty = Short.parseShort(args.get(1));
} catch(Exception ex){
Messages.PARAM_INVALID.send(sender, args.get(1));
return false;
Expand All @@ -49,7 +49,7 @@ public boolean execute(CommandSender sender, LinkedList<String> args) {
}

try{
resultQty = Short.valueOf(args.get(3));
resultQty = Short.parseShort(args.get(3));
} catch(Exception ex){
Messages.PARAM_INVALID.send(sender, args.get(3));
return false;
Expand All @@ -66,6 +66,7 @@ public boolean execute(CommandSender sender, LinkedList<String> args) {
trade.setIngredient(request, requestQty);
trade.setResult(result, resultQty);
manager.addTrade(args.get(0), trade);
Messages.TRADE_ADDED.send(sender, args.get(0));

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public boolean execute(CommandSender sender, LinkedList<String> args) {
return false;
}
manager.deleteShop(id);
Messages.SHOP_DELETED.send(sender);
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public boolean execute(CommandSender sender, LinkedList<String> args) {
}

manager.removeTrade(args.get(0), num -1);
Messages.TRADE_REMOVED.send(sender, args.get(0));
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public LocationAdapter(Location loc){
z = loc.getZ();
yaw = loc.getYaw();
pitch = loc.getPitch();
worldId = loc.getWorld().getUID();
}

public Location toLocation(){
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version: '0.1.0.1'
description: Customize Villager shops
load: POSTWORLD
author: Aldrigo Raffaele
website: spigotmc.org
website: https://github.com/Raffa50/Spigot-VillagerShops
prefix: VillagerShops
api-version: 1.14

Expand Down

0 comments on commit 18ef86e

Please sign in to comment.