1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

[CI skip] More refactoring and documentation [javadocs]

This commit is contained in:
TheBusyBiscuit 2020-04-04 15:16:11 +02:00
parent dfa3eb14ea
commit 007a4c6f83
9 changed files with 116 additions and 70 deletions

View File

@ -50,7 +50,8 @@
#### Additions
* Added GEOResourceGenerationEvent
* Added SlimefunGuide-Options API
* Added 1.13 backwards compatibility
* Added ItemSettings API
* Added experimental 1.13 backwards compatibility
#### Changes

View File

@ -2,25 +2,54 @@ package io.github.thebusybiscuit.slimefun4.api.items;
import java.util.Optional;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import io.github.thebusybiscuit.slimefun4.implementation.items.armor.SlimefunArmorPiece;
import io.github.thebusybiscuit.slimefun4.implementation.tasks.ArmorTask;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
/**
* This class serves as a way of checking whether a {@link Player} has changed their armor
* between ticks. We do that by storing the hash of their armor and a reference to the
* corresponding {@link SlimefunArmorPiece} if such a correlation exists.
*
* This gives us a significant performance improvement as we only need to check for a
* {@link SlimefunArmorPiece} if the item diverged in the first place.
*
* @author TheBusyBiscuit
*
* @see SlimefunArmorPiece
* @see ArmorTask
*/
public final class HashedArmorpiece {
private int hash;
private Optional<SlimefunArmorPiece> item;
/**
* This initializes a new {@link HashedArmorpiece} with no {@link SlimefunArmorPiece}
* and a zero hash.
*/
public HashedArmorpiece() {
this.hash = 0;
this.item = Optional.empty();
}
/**
* This will update this {@link HashedArmorpiece} with the given {@link ItemStack}
* and the corresponding {@link SlimefunItem}
*
* @param stack
* The new armorpiece to be stored in this {@link HashedArmorpiece}
* @param item
* The {@link SlimefunItem} corresponding to the provided {@link ItemStack}, may be null
*/
public void update(ItemStack stack, SlimefunItem item) {
if (stack == null) {
if (stack == null || stack.getType() == Material.AIR) {
this.hash = 0;
}
else {
@ -39,8 +68,16 @@ public final class HashedArmorpiece {
}
}
/**
* This method checks whether the given {@link ItemStack} is no longer similar to the
* one represented by this {@link HashedArmorpiece}.
*
* @param stack
* The {@link ItemStack} to compare
* @return Whether the {@link HashedArmorpiece} and the given {@link ItemStack} mismatch
*/
public boolean hasDiverged(ItemStack stack) {
if (stack == null) {
if (stack == null || stack.getType() == Material.AIR) {
return hash == 0;
}
else {
@ -52,6 +89,12 @@ public final class HashedArmorpiece {
}
}
/**
* Returns the {@link SlimefunArmorPiece} that corresponds to this {@link HashedArmorpiece},
* or an empty {@link Optional}
*
* @return An {@link Optional} describing the result
*/
public Optional<SlimefunArmorPiece> getItem() {
return item;
}

View File

@ -149,6 +149,7 @@ public abstract class Network {
onClassificationChange(l, currentAssignment, classification);
}
steps += 1;
if (steps >= maxSteps) {
@ -173,6 +174,10 @@ public abstract class Network {
discoverNeighbors(l, 0.0, 0.0, -1.0);
}
/**
* This method runs the network visualizer which displays a {@link Particle} on
* every {@link Location} that this {@link Network} can connect to.
*/
public void display() {
Slimefun.runSync(() -> {
DustOptions options = new DustOptions(Color.BLUE, 2F);

View File

@ -17,42 +17,22 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.ChargableItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemUseHandler;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
import me.mrCookieSlime.Slimefun.api.energy.ItemEnergy;
public class MultiTool extends ChargableItem {
private static final String PREFIX = "mode.";
private static final float COST = 0.3F;
private Map<UUID, Integer> selectedMode = new HashMap<>();
private List<Integer> modes;
private final Map<UUID, Integer> selectedMode = new HashMap<>();
private final List<MultiToolMode> modes = new ArrayList<>();
public MultiTool(SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, String... items) {
super(Categories.TECH, item, recipeType, recipe, getKeys(items), getValues(items));
}
private static String[] getKeys(String... items) {
String[] keys = new String[items.length * 2];
super(Categories.TECH, item, recipeType, recipe);
for (int i = 0; i < items.length; i++) {
keys[i * 2] = PREFIX + i + ".enabled";
keys[i * 2 + 1] = PREFIX + i + ".item";
modes.add(new MultiToolMode(this, i, items[i]));
}
return keys;
}
private static Object[] getValues(String... items) {
Object[] values = new Object[items.length * 2];
for (int i = 0; i < items.length; i++) {
values[i * 2] = true;
values[i * 2 + 1] = items[i];
}
return values;
}
protected ItemUseHandler getItemUseHandler() {
@ -69,8 +49,7 @@ public class MultiTool extends ChargableItem {
if (charge >= COST) {
ItemEnergy.chargeItem(item, -COST);
String itemID = (String) Slimefun.getItemValue(getID(), "mode." + modes.get(index) + ".item");
SlimefunItem sfItem = SlimefunItem.getByID(itemID);
SlimefunItem sfItem = modes.get(index).getItem();
if (sfItem != null) {
sfItem.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(e));
@ -78,10 +57,9 @@ public class MultiTool extends ChargableItem {
}
}
else {
index++;
if (index == modes.size()) index = 0;
index = nextIndex(index);
SlimefunItem selectedItem = SlimefunItem.getByID((String) Slimefun.getItemValue(getID(), "mode." + modes.get(index) + ".item"));
SlimefunItem selectedItem = modes.get(index).getItem();
String itemName = selectedItem != null ? selectedItem.getItemName() : "Unknown";
SlimefunPlugin.getLocal().sendMessage(p, "messages.mode-change", true, msg -> msg.replace("%device%", "Multi Tool").replace("%mode%", ChatColor.stripColor(itemName)));
selectedMode.put(p.getUniqueId(), index);
@ -89,6 +67,21 @@ public class MultiTool extends ChargableItem {
};
}
private int nextIndex(int i) {
int index = i;
do {
index++;
if (index >= modes.size()) {
index = 0;
}
}
while (index != i && !modes.get(index).isEnabled());
return index;
}
private BlockBreakHandler getBlockBreakHandler() {
return (e, item, fortune, drops) -> {
if (isItem(item)) {
@ -108,24 +101,4 @@ public class MultiTool extends ChargableItem {
addItemHandler(getBlockBreakHandler());
}
@Override
public void postRegister() {
List<Integer> list = new ArrayList<>();
int i = 0;
while (Slimefun.getItemValue(this.getID(), PREFIX + i + ".enabled") != null) {
if ((boolean) Slimefun.getItemValue(this.getID(), PREFIX + i + ".enabled")) {
list.add(i);
}
i++;
}
this.modes = list;
}
public List<Integer> getModes() {
return this.modes;
}
}

View File

@ -0,0 +1,25 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
class MultiToolMode {
private final ItemSetting<String> item;
private final ItemSetting<Boolean> enabled;
MultiToolMode(MultiTool multiTool, int id, String itemId) {
this.item = new ItemSetting<>("mode." + id + ".item", itemId);
this.enabled = new ItemSetting<>("mode." + id + ".enabled", true);
multiTool.addItemSetting(item, enabled);
}
public SlimefunItem getItem() {
return SlimefunItem.getByID(item.getValue());
}
public boolean isEnabled() {
return enabled.getValue();
}
}

View File

@ -15,6 +15,8 @@ import io.github.thebusybiscuit.cscorelib2.collections.RandomizedSet;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem;
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.ElectricGoldPan;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.AutomatedPanningMachine;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Lists.SlimefunItems;
@ -23,6 +25,17 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemUseHandler;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* A {@link GoldPan} is a {@link SlimefunItem} which allows you to obtain various
* resources from Gravel.
*
* @author TheBusyBiscuit
*
* @see NetherGoldPan
* @see AutomatedPanningMachine
* @see ElectricGoldPan
*
*/
public class GoldPan extends SimpleSlimefunItem<ItemUseHandler> implements RecipeDisplayItem {
private final RandomizedSet<ItemStack> randomizer = new RandomizedSet<>();
@ -109,7 +122,7 @@ public class GoldPan extends SimpleSlimefunItem<ItemUseHandler> implements Recip
return recipes;
}
protected class GoldPanDrop extends ItemSetting<Integer> {
class GoldPanDrop extends ItemSetting<Integer> {
private final ItemStack output;

View File

@ -14,9 +14,4 @@ public class ChargableItem extends SlimefunItem {
super(category, item, recipeType, recipe);
}
@Deprecated
public ChargableItem(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, String[] keys, Object[] values) {
super(category, item, recipeType, recipe, null, keys, values);
}
}

View File

@ -114,17 +114,6 @@ public class SlimefunItem implements Placeable {
this.recipeOutput = recipeOutput;
}
@Deprecated
public SlimefunItem(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack recipeOutput, String[] keys, Object[] values) {
this(category, item, recipeType, recipe, recipeOutput);
if (keys != null && values != null && keys.length == values.length) {
for (int i = 0; i < keys.length; i++) {
itemSettings.add(new ItemSetting<>(keys[i], values[i]));
}
}
}
// Previously deprecated constructor, now only for internal purposes
protected SlimefunItem(Category category, ItemStack item, String id, RecipeType recipeType, ItemStack[] recipe) {
this.category = category;

View File

@ -130,7 +130,9 @@ public abstract class BlockMenuPreset extends ChestMenu {
}
for (int slot = 0; slot < 54; slot++) {
if (getMenuClickHandler(slot) != null) menu.addMenuClickHandler(slot, getMenuClickHandler(slot));
if (getMenuClickHandler(slot) != null) {
menu.addMenuClickHandler(slot, getMenuClickHandler(slot));
}
}
menu.addMenuOpeningHandler(getMenuOpeningHandler());