Skip to content

Commit

Permalink
fix #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Hbeau committed Dec 25, 2018
1 parent 849ff4b commit 14c9c25
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 243 deletions.
414 changes: 187 additions & 227 deletions logs/latest.log

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions src/main/java/com/onaple/itemizer/Itemizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.onaple.itemizer.commands.globalConfiguration.ConfigureEnchantCommand;
import com.onaple.itemizer.commands.globalConfiguration.ConfigureModifierCommand;
import com.onaple.itemizer.commands.globalConfiguration.ConfigureRewriteCommand;
import com.onaple.itemizer.data.access.ItemDAO;
import com.onaple.itemizer.data.beans.ItemBean;
import com.onaple.itemizer.data.handlers.ConfigurationHandler;
import com.onaple.itemizer.events.ItemizerPreLoadEvent;
import com.onaple.itemizer.service.ItemService;
Expand Down Expand Up @@ -73,6 +75,18 @@ public static Logger getLogger() {
@Inject
private CraftRegister craftRegister;


private static ItemDAO itemDAO;
@Inject
private void setItemDAO(ItemDAO itemDAO) {
this.itemDAO = itemDAO;
}

public static ItemDAO getItemDAO(){
return itemDAO;
}


private static ConfigurationHandler configurationHandler;

@Inject
Expand Down Expand Up @@ -129,7 +143,7 @@ public void onServerStart(GameStartedServerEvent event) throws Exception {

CommandSpec retrieve = CommandSpec.builder()
.description(Text.of("Retrieve an item from a configuration file with its id."))
.arguments(GenericArguments.onlyOne(GenericArguments.string(Text.of("id"))),
.arguments(GenericArguments.onlyOne(GenericArguments.choices(Text.of("id"),itemDAO.getMap())),
GenericArguments.optional(GenericArguments.player(Text.of("player")))
)
.permission("itemizer.command.retrieve")
Expand Down Expand Up @@ -168,8 +182,8 @@ public void onServerStart(GameStartedServerEvent event) throws Exception {
CommandSpec rewrite = CommandSpec.builder()
.description(Text.of("Update global configuration."))
.arguments(
GenericArguments.onlyOne(GenericArguments.choices(Text.of("Key"),
globalConfig.getRewriteChoice()::keySet, globalConfig.getRewriteChoice()::get)),
GenericArguments.onlyOne(GenericArguments.string(Text.of("Key")/*,
globalConfig.getRewriteChoice()::keySet, globalConfig.getRewriteChoice()::get)*/)),
GenericArguments.optional(GenericArguments.string(Text.of("Name")))
)
.permission("itemizer.command.rewrite")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public class RetrieveCommand implements CommandExecutor {
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
String itemId = args.<String>getOne("id").orElse("");
Optional<ItemBean> optionalItem = args.<ItemBean>getOne("id");
Optional<Player> targetOptional = args.getOne("player");
Player target ;
if (targetOptional.isPresent()){
Expand All @@ -38,18 +38,14 @@ public CommandResult execute(CommandSource src, CommandContext args) throws Comm
return CommandResult.empty();
}
}
Optional<ItemBean> optionalItem = ItemDAO.getItem(itemId);
if (optionalItem.isPresent()) {
Optional<ItemStack> optionalItemStack = new ItemBuilder().buildItemStack(optionalItem.get());
if (optionalItemStack.isPresent()) {
target.getInventory().offer(optionalItemStack.get());
} else {
src.sendMessage(Text.of("Item " + itemId + " not valid."));
src.sendMessage(Text.of("Item " + optionalItem.get().getId() + " not valid."));
}
} else {
src.sendMessage(Text.of("Item " + itemId + " not found."));
}

return CommandResult.empty();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/onaple/itemizer/data/OnaKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class OnaKeys {
public static Key<Value<Integer>> HIDDEN_FLAGS = null;


public OnaKeys() {
HIDDEN_FLAGS = Key.builder()
.id("onaple:hiddenflags")
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/com/onaple/itemizer/data/access/ItemDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@
import com.onaple.itemizer.data.beans.ItemBean;
import com.onaple.itemizer.data.handlers.ConfigurationHandler;

import javax.inject.Singleton;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Singleton
public class ItemDAO {
public ItemDAO() {
}

/**
* Call the configuration handler to retrieve an item from an id
* @param id ID of the item
* @return Optional of the item data
*/
public static Optional<ItemBean> getItem(String id) {

public Optional<ItemBean> getItem(String id) {
List<ItemBean> items = Itemizer.getConfigurationHandler().getItemList();
for(ItemBean item: items) {
if (item.getId().equals(id)) {
Expand All @@ -22,4 +32,9 @@ public static Optional<ItemBean> getItem(String id) {
}
return Optional.empty();
}

public Map<String,ItemBean> getMap(){
List<ItemBean> items = Itemizer.getConfigurationHandler().getItemList();
return items.stream().collect(Collectors.toMap(ItemBean::getId, Function.identity()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ICraftRecipes deserialize(TypeToken<?> type, ConfigurationNode value) thr
public ItemStack getItemStack(ConfigurationNode node) throws ObjectMappingException {
String ref = node.getNode("ref").getString();
if(ref != null && !ref.equals("")) {
Optional<ItemBean> itemBeanOptional = ItemDAO.getItem(ref);
Optional<ItemBean> itemBeanOptional = Itemizer.getItemDAO().getItem(ref);
ItemStack result;
if (itemBeanOptional.isPresent()) {
Optional<ItemStack> itemStackOptional = new ItemBuilder().buildItemStack(itemBeanOptional.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PoolBean deserialize(TypeToken<?> type, ConfigurationNode value) throws O
String itemType = itemNode.getNode("type").getString();
Optional<ItemBean> item = Optional.empty();
if (reference != null && !reference.equals("")) {
item = ItemDAO.getItem(reference);
item = Itemizer.getItemDAO().getItem(reference);
}
if (!item.isPresent() && itemType != null){
item = Optional.of(new ItemBean(itemType));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.onaple.itemizer.service;

import com.onaple.itemizer.data.beans.ItemBean;
import com.onaple.itemizer.data.beans.ItemLoreWriter;
import org.spongepowered.api.item.inventory.ItemStack;

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/onaple/itemizer/service/ItemService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.onaple.itemizer.service;

import com.onaple.itemizer.Itemizer;
import com.onaple.itemizer.data.access.ItemDAO;
import com.onaple.itemizer.data.beans.ItemBean;
import com.onaple.itemizer.data.beans.ItemLoreWriter;
Expand Down Expand Up @@ -58,7 +59,7 @@ public Optional<ItemStack> fetch(String id) {

@Override
public Optional<ItemStack> retrieve(String id) {
Optional<ItemBean> optionalItem = ItemDAO.getItem(id);
Optional<ItemBean> optionalItem = Itemizer.getItemDAO().getItem(id);
if (optionalItem.isPresent()) {
return new ItemBuilder().buildItemStack(optionalItem.get());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/onaple/itemizer/utils/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private void enchantItemStack(ItemBean itemBean, boolean rewrite) {
type(optionalEnchant.get()).
level(enchant.getValue()).build()));
if(rewrite) {
if(config.getEnchantRewrite().size()>0) {
if(config.getEnchantRewrite().containsKey(optionalEnchant.get())) {
Itemizer.getLogger().info(config.getEnchantRewrite().get( optionalEnchant.get()));
lore.add(Text
.builder(config.getEnchantRewrite().get( optionalEnchant.get())+ " " + enchant.getValue())
Expand Down
4 changes: 2 additions & 2 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Mon Dec 24 08:58:45 CET 2018
#Tue Dec 25 23:18:36 CET 2018
MAJOR=1
MINOR=5
PATCH=0
PRE_RELEASE=
BUILD=185
BUILD=194
CODE=0

0 comments on commit 14c9c25

Please sign in to comment.