mirror of
https://github.com/ChestShop-authors/ChestShop-3.git
synced 2025-02-11 07:51:20 +01:00
Get rid of google dependencies
Mostly replaced with Java 8 features and our own cache
This commit is contained in:
parent
9b7000272d
commit
1d80edbf9b
@ -1,13 +1,12 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import java.lang.annotation.AnnotationFormatError;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Parses an entity (class with database fields)
|
||||
@ -37,7 +36,7 @@ public class EntityParser {
|
||||
fields.add(convertToSQL(field));
|
||||
}
|
||||
|
||||
return Joiner.on(',').join(fields);
|
||||
return fields.stream().collect(Collectors.joining(","));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,8 +1,11 @@
|
||||
package com.Acrobot.Breeze.Database;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
import java.sql.*;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.ResultSetMetaData;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Represents a table in database
|
||||
@ -129,10 +132,12 @@ public class Table {
|
||||
String statement;
|
||||
|
||||
if (condition == null || condition.isEmpty()) {
|
||||
String format = '\'' + Joiner.on("', ").join(row.getValues()) + '\'';
|
||||
String format = '\'' + row.getValues().stream().collect(Collectors.joining("', ")) + '\'';
|
||||
statement = String.format(INSERT_VALUES, format);
|
||||
} else {
|
||||
String format = Joiner.on("', ").withKeyValueSeparator("= '").join(row.getKeysAndValues()) + '\'';
|
||||
String format = row.getKeysAndValues().entrySet().stream()
|
||||
.map(e -> e.getKey() + "= '" + e.getValue() + "'")
|
||||
.collect(Collectors.joining(", "));
|
||||
statement = String.format(UPDATE, format, condition);
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.Acrobot.Breeze.Utils;
|
||||
|
||||
import com.Acrobot.ChestShop.ChestShop;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
import info.somethingodd.OddItem.OddItem;
|
||||
import org.bukkit.CoalType;
|
||||
import org.bukkit.DyeColor;
|
||||
@ -166,7 +164,10 @@ public class MaterialUtil {
|
||||
return itemStack;
|
||||
}
|
||||
|
||||
String[] split = Iterables.toArray(Splitter.onPattern(":|-|#").trimResults().split(itemName), String.class);
|
||||
String[] split = itemName.split("[:\\-#]");
|
||||
for (int i = 0; i < split.length; i++) {
|
||||
split[i] = split[i].trim();
|
||||
}
|
||||
|
||||
Material material = getMaterial(split[0]);
|
||||
short durability = getDurability(itemName);
|
||||
|
@ -1,12 +1,12 @@
|
||||
package com.Acrobot.Breeze.Utils.MojangAPI;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -20,7 +20,7 @@ public class NameFetcher implements Callable<Map<UUID, String>> {
|
||||
private final List<UUID> uuids;
|
||||
|
||||
public NameFetcher(UUID... uuids) {
|
||||
this.uuids = ImmutableList.copyOf(uuids);
|
||||
this.uuids = Arrays.asList(uuids);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.Acrobot.Breeze.Utils.MojangAPI;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
@ -28,11 +27,7 @@ public class UUIDFetcher implements Callable<Map<String, UUID>> {
|
||||
private final List<String> names;
|
||||
|
||||
public UUIDFetcher(String... names) {
|
||||
this.names = ImmutableList.copyOf(names);
|
||||
}
|
||||
|
||||
public UUIDFetcher(String name) {
|
||||
this.names = ImmutableList.of(name);
|
||||
this.names = Arrays.asList(names);
|
||||
}
|
||||
|
||||
public Map<String, UUID> call() throws Exception {
|
||||
|
@ -2,9 +2,9 @@ package com.Acrobot.Breeze.Utils;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.MojangAPI.NameFetcher;
|
||||
import com.Acrobot.Breeze.Utils.MojangAPI.UUIDFetcher;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -60,7 +60,7 @@ public class NameUtil {
|
||||
try {
|
||||
return fetcher.call();
|
||||
} catch (Exception exception) {
|
||||
return ImmutableMap.of();
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@ -94,7 +94,7 @@ public class NameUtil {
|
||||
try {
|
||||
return fetcher.call();
|
||||
} catch (Exception e) {
|
||||
return ImmutableMap.of();
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
package com.Acrobot.Breeze.Utils;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import org.apache.commons.lang.WordUtils;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @author Acrobot
|
||||
@ -43,7 +45,7 @@ public class StringUtil {
|
||||
* @return Joined array
|
||||
*/
|
||||
public static String joinArray(String[] array) {
|
||||
return Joiner.on(' ').join(array);
|
||||
return joinArray(Arrays.asList(array));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -52,8 +54,8 @@ public class StringUtil {
|
||||
* @param array Iterable
|
||||
* @return Joined iterable
|
||||
*/
|
||||
public static String joinArray(Iterable<?> array) {
|
||||
return Joiner.on(' ').join(array);
|
||||
public static String joinArray(Collection<String> array) {
|
||||
return array.stream().collect(Collectors.joining(" "));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,7 +7,6 @@ import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
|
||||
import com.Acrobot.ChestShop.Permission;
|
||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||
import com.Acrobot.ChestShop.Utils.uBlock;
|
||||
import com.google.common.collect.Lists;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
@ -165,7 +164,7 @@ public class SignBreak implements Listener {
|
||||
|
||||
private static List<Sign> getAttachedSigns(Block block) {
|
||||
if (block == null) {
|
||||
return Lists.newArrayList();
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
if (isSign(block)) {
|
||||
|
@ -2,13 +2,13 @@ package com.Acrobot.ChestShop.Listeners.PostTransaction;
|
||||
|
||||
import com.Acrobot.Breeze.Utils.InventoryUtil;
|
||||
import com.Acrobot.Breeze.Utils.MaterialUtil;
|
||||
import com.Acrobot.Breeze.Utils.StringUtil;
|
||||
import com.Acrobot.ChestShop.Commands.Toggle;
|
||||
import com.Acrobot.ChestShop.Configuration.Messages;
|
||||
import com.Acrobot.ChestShop.Configuration.Properties;
|
||||
import com.Acrobot.ChestShop.Economy.Economy;
|
||||
import com.Acrobot.ChestShop.Events.TransactionEvent;
|
||||
import com.Acrobot.ChestShop.UUIDs.NameManager;
|
||||
import com.google.common.base.Joiner;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -16,6 +16,8 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
@ -80,14 +82,13 @@ public class TransactionMessageSender implements Listener {
|
||||
private static String parseItemInformation(ItemStack[] items) {
|
||||
ItemStack[] stock = InventoryUtil.mergeSimilarStacks(items);
|
||||
|
||||
StringBuilder message = new StringBuilder(15);
|
||||
Joiner joiner = Joiner.on(' ');
|
||||
List<String> itemText = new ArrayList<>();
|
||||
|
||||
for (ItemStack item : stock) {
|
||||
joiner.appendTo(message, item.getAmount(), MaterialUtil.getName(item));
|
||||
itemText.add(item.getAmount() + " " + MaterialUtil.getName(item));
|
||||
}
|
||||
|
||||
return message.toString();
|
||||
return StringUtil.joinArray(itemText);
|
||||
}
|
||||
|
||||
private static void sendMessageToOwner(String message, TransactionEvent event) {
|
||||
|
@ -8,8 +8,6 @@ import com.Acrobot.ChestShop.Database.Account;
|
||||
import com.Acrobot.ChestShop.Database.DaoCreator;
|
||||
import com.Acrobot.ChestShop.Permission;
|
||||
import com.Acrobot.ChestShop.Signs.ChestShopSign;
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.j256.ormlite.dao.Dao;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
@ -19,7 +17,10 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@ -32,9 +33,9 @@ import java.util.logging.Level;
|
||||
public class NameManager {
|
||||
private static Dao<Account, String> accounts;
|
||||
|
||||
private static Cache<String, Account> usernameToAccount = CacheBuilder.newBuilder().maximumSize(Properties.CACHE_SIZE).build();
|
||||
private static Cache<UUID, Account> uuidToAccount = CacheBuilder.newBuilder().maximumSize(Properties.CACHE_SIZE).build();
|
||||
private static Cache<String, Account> shortToAccount = CacheBuilder.newBuilder().maximumSize(Properties.CACHE_SIZE).build();
|
||||
private static SimpleLoadingCache<String, Account> usernameToAccount = new SimpleLoadingCache<>(Properties.CACHE_SIZE);
|
||||
private static SimpleLoadingCache<UUID, Account> uuidToAccount = new SimpleLoadingCache<>(Properties.CACHE_SIZE);
|
||||
private static SimpleLoadingCache<String, Account> shortToAccount = new SimpleLoadingCache<>(Properties.CACHE_SIZE);
|
||||
|
||||
/**
|
||||
* Get account info from a UUID
|
||||
@ -283,4 +284,44 @@ public class NameManager {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static class SimpleLoadingCache<K, V> {
|
||||
private final LinkedHashMap<K, V> map;
|
||||
|
||||
public SimpleLoadingCache(int cacheSize) {
|
||||
map = new LinkedHashMap<K, V>(cacheSize * 10/9, 0.7f, true) {
|
||||
@Override
|
||||
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
|
||||
return size() > cacheSize;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void put(K key, V value) {
|
||||
map.put(key, value);
|
||||
}
|
||||
|
||||
public V get(K key) {
|
||||
return map.get(key);
|
||||
}
|
||||
|
||||
public V get(K key, Callable<? extends V> loader) throws ExecutionException {
|
||||
if (contains(key)) {
|
||||
return map.get(key);
|
||||
}
|
||||
try {
|
||||
V value = loader.call();
|
||||
if (value != null) {
|
||||
put(key, value);
|
||||
}
|
||||
return value;
|
||||
} catch (Exception e) {
|
||||
throw new ExecutionException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean contains(K key) {
|
||||
return map.containsKey(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user