Skip to content

Commit

Permalink
2.0重制版完成
Browse files Browse the repository at this point in the history
  • Loading branch information
haiman233 authored Apr 6, 2022
2 parents 77ec392 + 71535d2 commit 996ebdd
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<dependency>
<groupId>net.guizhanss</groupId>
<artifactId>GuizhanLib</artifactId>
<version>0.8.1</version>
<version>0.8.2</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/bxx2004/bump/BumpLocalization.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public BumpLocalization(Bump plugin) {
super(plugin);
}

@ParametersAreNonnullByDefault
public @Nonnull String getString(String key, Object... args) {
return MessageFormat.format(getString(key), args);
}

public @Nonnull String getCategoryName(@Nonnull String categoryId) {
Validate.notNull(categoryId, "Category Id cannot be null");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void appraise(@Nonnull BlockMenu blockMenu, @Nonnull Player p) {
}

private boolean validate(ItemStack itemStack) {
return AppraiseUtils.isAppraiseable(itemStack)
return AppraiseUtils.isAppraisable(itemStack)
|| SlimefunUtils.isItemSimilar(BumpItems.RANDOM_HELMET, itemStack, false)
|| SlimefunUtils.isItemSimilar(BumpItems.RANDOM_SWORD, itemStack, false);
}
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/bxx2004/bump/slimefun/items/tool/AppraisalPaper.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public ItemUseHandler getItemHandler() {
e.cancel();

Player p = e.getPlayer();
SlimefunItemStack paperItemStack = (SlimefunItemStack) e.getItem();
ItemStack paperItemStack = e.getItem();

ChestMenu menu = new ChestMenu(paperItemStack.getDisplayName());
ChestMenu menu = new ChestMenu(paperItemStack.getItemMeta().getDisplayName());
menu.setPlayerInventoryClickable(true);

// Setup menu
Expand All @@ -69,7 +69,7 @@ public ItemUseHandler getItemHandler() {
// Block Appraisal paper click
menu.addPlayerInventoryClickHandler((player, slot, item, action) -> {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
return sfItem instanceof AppraisalPaper;
return !(sfItem instanceof AppraisalPaper);
});

// Add appraise button handler
Expand All @@ -80,31 +80,39 @@ public ItemUseHandler getItemHandler() {

if (input == null) {
Bump.getLocalization().sendMessage(p, "no-input");
return true;
return false;
}

// Check output slot
if (menu.getItemInSlot(OUTPUT_SLOT) != null) {
Bump.getLocalization().sendMessage(p, "output-no-space");
return true;
return false;
}

// validate item
/*
Validate the item. The item that can be marked appraisable
should meet these requirements:
- is appraisable type (sword, armors for now)
- is a slimefun item
- has not been appraised yet
- has not beed marked appraisable yet
*/
if (AppraiseUtils.isAppraisableMaterial(input.getType())
&& sfItem != null
&& !AppraiseUtils.isAppraised(input)) {
&& !AppraiseUtils.isAppraised(input)
&& !AppraiseUtils.isAppraisable(input)) {
// item can be marked appraisable
ItemStack output = input.clone();
AppraiseUtils.setAppraisable(output);
menu.replaceExistingItem(INPUT_SLOT, null);
menu.addItem(OUTPUT_SLOT, output);
menu.replaceExistingItem(OUTPUT_SLOT, output);

Bump.getLocalization().sendMessage(p, "tool.appraisal_paper.success");
} else {
Bump.getLocalization().sendMessage(p, "tool.appraisal_paper.invalid");
}

return true;
return false;
});

menu.open(p);
Expand Down
102 changes: 99 additions & 3 deletions src/main/java/bxx2004/bump/util/AppraiseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import net.guizhanss.guizhanlib.utils.ChatUtil;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeModifier;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataContainer;
Expand All @@ -13,6 +16,9 @@
import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;

/**
* Utility methods for appraise
Expand Down Expand Up @@ -43,13 +49,13 @@ public static boolean isAppraisableMaterial(@Nonnull Material type) {
*
* @return if the {@link ItemStack} can be used in appraisal machine
*/
public static boolean isAppraiseable(@Nonnull ItemStack itemStack) {
public static boolean isAppraisable(@Nonnull ItemStack itemStack) {
Validate.notNull(itemStack, "itemStack should not be null");
Validate.notNull(itemStack.getItemMeta(), "itemMeta should not be null");

PersistentDataContainer pdc = itemStack.getItemMeta().getPersistentDataContainer();
return pdc.has(Keys.APPRAISE_LEVEL, PersistentDataType.BYTE) &&
pdc.get(Keys.APPRAISE_LEVEL, PersistentDataType.BYTE) == 1;
return pdc.has(Keys.APPRAISABLE, PersistentDataType.BYTE)
&& pdc.get(Keys.APPRAISABLE, PersistentDataType.BYTE) == 1;
}

/**
Expand All @@ -70,6 +76,7 @@ public static void setAppraisable(@Nonnull ItemStack itemStack) {
} else {
lore = new ArrayList<>();
}
lore.add("");
lore.add(ChatUtil.color(Bump.getLocalization().getString("lores.not-appraised")));
im.setLore(lore);

Expand All @@ -94,10 +101,99 @@ public static boolean isAppraised(@Nonnull ItemStack itemStack) {
return itemStack.getItemMeta().getPersistentDataContainer().has(Keys.APPRAISE_LEVEL, PersistentDataType.BYTE);
}

/**
* Apply appraisal result to {@link ItemStack}
*
* @param itemStack the {@link ItemStack} to be dealt with
*/
public static void applyAppraise(@Nonnull ItemStack itemStack) {
Validate.notNull(itemStack, "itemStack should not be null");
Validate.notNull(itemStack.getItemMeta(), "itemMeta should not be null");

ItemMeta im = itemStack.getItemMeta();
PersistentDataContainer pdc = im.getPersistentDataContainer();
EquipmentSlot slot = getEquipmentSlot(itemStack.getType());
int stars = 0;

if (MinecraftTag.SWORD.isTagged(itemStack)) {
// swords can be applied with damage and attack apeed modifier
double damage = ThreadLocalRandom.current().nextDouble(3, 15);
double attackSpeed = ThreadLocalRandom.current().nextDouble(0, 0.8);
im.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, new AttributeModifier(UUID.randomUUID(), "DAMAGE", damage, AttributeModifier.Operation.ADD_NUMBER, slot));
im.addAttributeModifier(Attribute.GENERIC_ATTACK_SPEED, new AttributeModifier(UUID.randomUUID(), "SPEED", attackSpeed, AttributeModifier.Operation.ADD_NUMBER, slot));

// the star is determined by damage only
stars = getLevelByLimit(damage, 3, 15);
} else if (MinecraftTag.ARMOR.isTagged(itemStack)) {
// armor can be applied with armor modifier
double armor = ThreadLocalRandom.current().nextDouble(3, 15);
im.addAttributeModifier(Attribute.GENERIC_ARMOR, new AttributeModifier(UUID.randomUUID(), "ARMOR", armor, AttributeModifier.Operation.ADD_NUMBER, slot));
im.addAttributeModifier(Attribute.GENERIC_ARMOR_TOUGHNESS, new AttributeModifier(UUID.randomUUID(), "ARMOR_TOUGHNESS", 0, AttributeModifier.Operation.ADD_NUMBER, slot));

// the star is determined by armor only
stars = getLevelByLimit(armor, 3, 15);
}

// set lore
List<String> lore = im.getLore();
for (int i = 0; i < lore.size(); i++) {
if (lore.get(i).equals(ChatUtil.color(Bump.getLocalization().getString("lores.not-appraised")))) {
String line = Bump.getLocalization().getString("lores.appraised", Utils.getStars(stars));
lore.set(i, ChatUtil.color(line));
break;
}
}
im.setLore(lore);

// set pdc
pdc.set(Keys.APPRAISE_LEVEL, PersistentDataType.BYTE, (byte) stars);

itemStack.setItemMeta(im);
}

/**
* Get the available {@link EquipmentSlot} of modifier for a given {@link Material} type
*
* @param type the {@link Material} to be analyzed
*
* @return an available {@link EquipmentSlot}
*/
private static EquipmentSlot getEquipmentSlot(Material type) {
if (MinecraftTag.SWORD.isTagged(type)) {
return EquipmentSlot.HAND;
} else if (MinecraftTag.HELMET.isTagged(type)) {
return EquipmentSlot.HEAD;
} else if (MinecraftTag.CHESTPLATE.isTagged(type)) {
return EquipmentSlot.CHEST;
} else if (MinecraftTag.LEGGINGS.isTagged(type)) {
return EquipmentSlot.LEGS;
} else if (MinecraftTag.BOOTS.isTagged(type)) {
return EquipmentSlot.FEET;
} else {
return EquipmentSlot.OFF_HAND;
}
}

/**
* Get the appraise level
*
* @param randomValue random generated value
* @param min minimum value
* @param max maximum value
* @return appraise level
*/
private static int getLevelByLimit(double randomValue, int min, int max) {
double percent = (randomValue - min) / (max - min) * 100;
if (percent >= 95) {
return 5;
} else if (percent >= 80) {
return 4;
} else if (percent >= 60) {
return 3;
} else if (percent >= 30) {
return 2;
} else {
return 1;
}
}
}
36 changes: 34 additions & 2 deletions src/main/java/bxx2004/bump/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import bxx2004.bump.Bump;
import net.guizhanss.guizhanlib.utils.ChatUtil;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.inventory.ItemStack;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
Expand Down Expand Up @@ -64,14 +69,41 @@ public static void changeFoodLevel(Player p, int level) {
/**
* Try to push {@link ItemStack} to player's inventory,
* if full, then drop on the ground
*
* @param p the {@link Player} to be dealt with
* @param itemStacks all {@link ItemStack} to be pushed
*/
public static void pushToPlayerInventory(Player p, ItemStack... itemStacks) {
Map<Integer, ItemStack> remainingItemMap = p.getInventory().addItem(itemStacks);
public static void pushToPlayerInventory(@Nonnull Player p, @Nonnull ItemStack... itemStacks) {
Validate.notNull(p, "player should not be null");
Validate.notNull(itemStacks, "at least one ItemStack is needed");

// filter null ItemStacks
List<ItemStack> items = new ArrayList<>();
for (ItemStack item : itemStacks) {
if (item != null) {
items.add(item);
}
}
Map<Integer, ItemStack> remainingItemMap = p.getInventory().addItem(items.toArray(new ItemStack[0]));

for (ItemStack item : remainingItemMap.values()) {
p.getWorld().dropItem(p.getLocation(), item.clone());
}
}

/**
* Get a {@link String} of consecutive stars
*
* @param n the number of stars
*
* @return {@link String} of consecutive stars
*/
public static @Nonnull String getStars(int n) {
StringBuilder builder = new StringBuilder();
while (n > 0) {
builder.append("⭐");
n--;
}
return builder.toString();
}
}

0 comments on commit 996ebdd

Please sign in to comment.