Skip to content

Commit

Permalink
refactor: some more changes I'm too lazy to document
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRONiN committed Mar 13, 2023
1 parent 3d50321 commit bfee686
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 108 deletions.
1 change: 1 addition & 0 deletions src/main/java/in/arcadelabs/labaide/LabAide.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class LabAide extends JavaPlugin {
private final MiniMessage miniMessage = MiniMessage.builder().build();
private final List<Plugin> dependants = new ArrayList<>();
private BStats metrics;

public static Logger Logger() {
return logger;
}
Expand Down
38 changes: 17 additions & 21 deletions src/main/java/in/arcadelabs/labaide/NameValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.google.gson.JsonParser;
import in.arcadelabs.labaide.json.JsonFetcher;
import lombok.experimental.UtilityClass;
import org.json.simple.JSONObject;

import java.io.IOException;
import java.net.URL;
Expand All @@ -14,30 +13,27 @@
@UtilityClass
public class NameValidator {

public boolean offlineCheck(final String username) {
if (username.length() < 3 || username.length() > 16) return false;
if (username.contains(" ")) return false;
public boolean offlineCheck(final String username) {
if (username.length() < 3 || username.length() > 16) return false;
if (username.contains(" ")) return false;

Pattern p = Pattern.compile("[^a-z0-9_]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(username);
Pattern p = Pattern.compile("[^a-z0-9_]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(username);

if (m.matches()) return false;
return true;
}
return !m.matches();
}

public boolean geyserCheck(final String username, final String geyserPrefix) {
if (!offlineCheck(username)) return false;
if (!username.startsWith(geyserPrefix)) return false;
return true;
}
public boolean geyserCheck(final String username, final String geyserPrefix) {
if (!offlineCheck(username)) return false;
return username.startsWith(geyserPrefix);
}

public boolean onlineCheck(final String username) throws IOException {
if (!offlineCheck(username)) return false;
public boolean onlineCheck(final String username) throws IOException {
if (!offlineCheck(username)) return false;

String json = JsonFetcher.getJsonString(new URL("https://api.mojang.com/users/profiles/minecraft/" + username));
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
String json = JsonFetcher.getJsonString(new URL("https://api.mojang.com/users/profiles/minecraft/" + username));
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();

if (!jsonObject.has("id")) return false;
return true;
}
return jsonObject.has("id");
}
}
86 changes: 22 additions & 64 deletions src/main/java/in/arcadelabs/labaide/cooldown/CooldownManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,89 +18,47 @@

package in.arcadelabs.labaide.cooldown;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.UUID;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class CooldownManager {
private final HashMap<UUID, Long> cooldownMap;
private long cooldownPeriod;
public class CooldownManager<T> {
private final Map<T, Long> cooldownMap;
private final long cooldownPeriod;

/**
* Instantiates a new Cooldown manager.
*
* @param seconds the seconds
*/
public CooldownManager(final int seconds) {
public CooldownManager(long cooldownPeriod, TimeUnit unit) {
this.cooldownMap = new HashMap<>();
this.cooldownPeriod = seconds * 1000L;
this.cooldownPeriod = unit.toMillis(cooldownPeriod);
}

/**
* On cooldown check.
*
* @param uuid the uuid
* @return the boolean
*/
public boolean isOnCooldown(final UUID uuid) {
if (!this.cooldownMap.containsKey(uuid)) return false;
return this.getRemainingTime(uuid) > 0;
public boolean isOnCooldown(@NotNull T key) {
return this.cooldownMap.containsKey(key) && this.cooldownMap.get(key) > System.currentTimeMillis();
}

/**
* Sets uuid on cooldown.
*
* @param uuid the uuid
*/
public void setCooldown(final UUID uuid) {
this.cooldownMap.put(uuid, System.currentTimeMillis() + this.cooldownPeriod);
public void setCooldown(@NotNull T key) {
this.cooldownMap.put(key, System.currentTimeMillis() + this.cooldownPeriod);
}

/**
* Remove uuid's cooldown.
*
* @param uuid the uuid
*/
public void removeCooldown(final UUID uuid) {
if (!this.cooldownMap.containsKey(uuid)) return;
this.cooldownMap.remove(uuid);
public void removeCooldown(@NotNull T key) {
this.cooldownMap.remove(key);
}

/**
* Gets remaining time.
*
* @param uuid the uuid
* @return the remaining time
*/
public long getRemainingTime(final UUID uuid) {
if (!this.cooldownMap.containsKey(uuid)) return 0;
long difference = this.cooldownMap.get(uuid) - System.currentTimeMillis();
if (difference <= 0) return 0;
return TimeUnit.MILLISECONDS.toSeconds(difference);
public long getRemainingTime(@NotNull T key, @NotNull TimeUnit unit) {
long remaining = this.cooldownMap.containsKey(key) ? this.cooldownMap.get(key) - System.currentTimeMillis() : 0;
return unit.convert(Math.max(remaining, 0), TimeUnit.MILLISECONDS);
}

/**
* Sets cooldown time.
*
* @param seconds the cooldown time
*/
public void setCooldownPeriod(final int seconds) {
this.cooldownPeriod = seconds * 1000L;
public Map<T, Long> getCooldownMap() {
return cooldownMap;
}

/**
* Gets cooldown map.
*
* @return the cooldown map
*/
public HashMap<UUID, Long> getCooldownMap() {
return this.cooldownMap;
public long getCooldownPeriod() {
return cooldownPeriod;
}

/**
* Clear cooldowns.
*/
public void clearCooldowns() {
this.cooldownMap.clear();
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/in/arcadelabs/labaide/item/HeadBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class HeadBuilder {
private static final int END_MARK = 0;
private final Logger logger;
private final Logger.Level level;

public HeadBuilder(final Logger logger, final Logger.Level level) {
this.logger = logger;
this.level = level;
Expand Down Expand Up @@ -109,4 +110,4 @@ public Map<String, Object> createSkullMap(String value) {
return null;
}
}
}
}
27 changes: 15 additions & 12 deletions src/main/java/in/arcadelabs/labaide/item/ItemBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.List;
Expand All @@ -46,7 +47,7 @@ public class ItemBuilder {
*
* @param material the material
*/
public ItemBuilder(final Material material) {
public ItemBuilder(@NotNull final Material material) {
this.itemStack = new ItemStack(material);
this.itemMeta = this.itemStack.getItemMeta();
}
Expand All @@ -57,7 +58,7 @@ public ItemBuilder(final Material material) {
* @param material the material
* @param textureMap the texture map
*/
public ItemBuilder(final Material material, final Map<String, Object> textureMap) {
public ItemBuilder(@NotNull final Material material, @NotNull final Map<String, Object> textureMap) {
this.itemStack = new ItemStack(material);
this.itemMeta = (ItemMeta) ConfigurationSerialization.deserializeObject(textureMap);
}
Expand All @@ -68,7 +69,7 @@ public ItemBuilder(final Material material, final Map<String, Object> textureMap
* @param name the name
* @return the name
*/
public ItemBuilder setName(final Component name) {
public ItemBuilder setName(@NotNull final Component name) {
this.itemMeta.displayName(name);
return this;
}
Expand All @@ -79,7 +80,7 @@ public ItemBuilder setName(final Component name) {
* @param loreList the lore list
* @return the lore
*/
public ItemBuilder setLore(final List<Component> loreList) {
public ItemBuilder setLore(@NotNull final List<Component> loreList) {
this.itemMeta.lore(loreList);
return this;
}
Expand Down Expand Up @@ -109,12 +110,14 @@ public ItemBuilder setUnbreakable(final boolean unbreakable) {
/**
* Sets pdc object.
*
* @param <T> the type parameter
* @param <Z> the type parameter
* @param namespacedKey the namespaced key
* @param type the type
* @param value the value
* @return the pdc object
*/
public ItemBuilder setPDCObject(final NamespacedKey namespacedKey, final PersistentDataType type, final Object value) {
public <T, Z> ItemBuilder setPDCObject(@NotNull final NamespacedKey namespacedKey, @NotNull final PersistentDataType<T, Z> type, @NotNull final Z value) {
this.itemMeta.getPersistentDataContainer().set(namespacedKey, type, value);
return this;
}
Expand All @@ -127,7 +130,7 @@ public ItemBuilder setPDCObject(final NamespacedKey namespacedKey, final Persist
* @param ignoreLevelRestriction the ignore level restriction
* @return the item builder
*/
public ItemBuilder addEnchantment(final Enchantment enchantment, final int level, final boolean ignoreLevelRestriction) {
public ItemBuilder addEnchantment(@NotNull final Enchantment enchantment, final int level, final boolean ignoreLevelRestriction) {
this.itemMeta.addEnchant(enchantment, level, ignoreLevelRestriction);
return this;
}
Expand All @@ -138,7 +141,7 @@ public ItemBuilder addEnchantment(final Enchantment enchantment, final int level
* @param itemFlags the item flags
* @return the item builder
*/
public ItemBuilder addItemFlags(final ItemFlag... itemFlags) {
public ItemBuilder addItemFlags(@NotNull final ItemFlag... itemFlags) {
this.itemMeta.addItemFlags(itemFlags);
return this;
}
Expand All @@ -150,7 +153,7 @@ public ItemBuilder addItemFlags(final ItemFlag... itemFlags) {
* @param modifier the modifier
* @return the item builder
*/
public ItemBuilder addAttributeModifier(final Attribute attribute, final AttributeModifier modifier) {
public ItemBuilder addAttributeModifier(@NotNull final Attribute attribute, @NotNull final AttributeModifier modifier) {
this.itemMeta.addAttributeModifier(attribute, modifier);
return this;
}
Expand All @@ -161,7 +164,7 @@ public ItemBuilder addAttributeModifier(final Attribute attribute, final Attribu
* @param attributeModifiers the attribute modifiers
* @return the attribute modifiers
*/
public ItemBuilder setAttributeModifiers(final Multimap<Attribute, AttributeModifier> attributeModifiers) {
public ItemBuilder setAttributeModifiers(@NotNull final Multimap<Attribute, AttributeModifier> attributeModifiers) {
this.itemMeta.setAttributeModifiers(attributeModifiers);
return this;
}
Expand All @@ -172,7 +175,7 @@ public ItemBuilder setAttributeModifiers(final Multimap<Attribute, AttributeModi
* @param canDestroy the can destroy
* @return the destroyable keys
*/
public ItemBuilder setDestroyableKeys(final Collection<Namespaced> canDestroy) {
public ItemBuilder setDestroyableKeys(@NotNull final Collection<Namespaced> canDestroy) {
this.itemMeta.setDestroyableKeys(canDestroy);
return this;
}
Expand All @@ -183,7 +186,7 @@ public ItemBuilder setDestroyableKeys(final Collection<Namespaced> canDestroy) {
* @param canPlaceOn the can place on
* @return the placeable keys
*/
public ItemBuilder setPlaceableKeys(final Collection<com.destroystokyo.paper.Namespaced> canPlaceOn) {
public ItemBuilder setPlaceableKeys(@NotNull final Collection<com.destroystokyo.paper.Namespaced> canPlaceOn) {
this.itemMeta.setPlaceableKeys(canPlaceOn);
return this;
}
Expand All @@ -206,4 +209,4 @@ public ItemBuilder build() {
public ItemStack getBuiltItem() {
return this.itemStack;
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/in/arcadelabs/labaide/json/JsonFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
@UtilityClass
public class JsonFetcher {

public String getJsonString(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder buffer = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
public String getJsonString(URL url) throws IOException {
URLConnection urlConnection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder buffer = new StringBuilder();
String line;

while ((line = reader.readLine()) != null) {
buffer.append(line);
}
return buffer.toString();
}

}

Expand Down

0 comments on commit bfee686

Please sign in to comment.