1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Refactored a bunch of android-related things

This commit is contained in:
TheBusyBiscuit 2020-06-13 01:50:48 +02:00
parent ef02433188
commit e45764ade1
7 changed files with 333 additions and 277 deletions

View File

@ -81,71 +81,10 @@ abstract class ChestTerminalNetwork extends Network {
switch (request.getDirection()) { switch (request.getDirection()) {
case INSERT: case INSERT:
ItemStack requestedItem = request.getItem(); distributeInsertionRequest(request, menu, iterator, destinations);
for (Location l : destinations) {
Optional<Block> target = getAttachedBlock(l.getBlock());
if (target.isPresent()) {
requestedItem = CargoUtils.insert(l.getBlock(), target.get(), requestedItem);
if (requestedItem == null) {
menu.replaceExistingItem(request.getSlot(), null);
break;
}
}
}
if (requestedItem != null) {
menu.replaceExistingItem(request.getSlot(), requestedItem);
}
iterator.remove();
break; break;
case WITHDRAW: case WITHDRAW:
int slot = request.getSlot(); collectExtractionRequest(request, menu, iterator, providers);
ItemStack prevStack = menu.getItemInSlot(slot);
if (!(prevStack == null || (prevStack.getAmount() + request.getItem().getAmount() <= prevStack.getMaxStackSize() && SlimefunUtils.isItemSimilar(prevStack, new CustomItem(request.getItem(), 1), true)))) {
iterator.remove();
break;
}
ItemStack stack = null;
ItemStack requested = request.getItem();
for (Location l : providers) {
Optional<Block> target = getAttachedBlock(l.getBlock());
if (target.isPresent()) {
ItemStack is = CargoUtils.withdraw(l.getBlock(), target.get(), requested);
if (is != null) {
if (stack == null) {
stack = is;
}
else {
stack = new CustomItem(stack, stack.getAmount() + is.getAmount());
}
if (is.getAmount() == requested.getAmount()) {
break;
}
else {
requested = new CustomItem(requested, requested.getAmount() - is.getAmount());
}
}
}
}
if (stack != null) {
ItemStack prev = menu.getItemInSlot(slot);
if (prev == null) menu.replaceExistingItem(slot, stack);
else menu.replaceExistingItem(slot, new CustomItem(stack, stack.getAmount() + prev.getAmount()));
}
iterator.remove();
break; break;
default: default:
break; break;
@ -154,6 +93,79 @@ abstract class ChestTerminalNetwork extends Network {
} }
} }
private void distributeInsertionRequest(ItemRequest request, BlockMenu terminal, Iterator<ItemRequest> iterator, Set<Location> destinations) {
ItemStack item = request.getItem();
for (Location l : destinations) {
Optional<Block> target = getAttachedBlock(l.getBlock());
if (target.isPresent()) {
item = CargoUtils.insert(l.getBlock(), target.get(), item);
if (item == null) {
terminal.replaceExistingItem(request.getSlot(), null);
break;
}
}
}
if (item != null) {
terminal.replaceExistingItem(request.getSlot(), item);
}
iterator.remove();
}
private void collectExtractionRequest(ItemRequest request, BlockMenu terminal, Iterator<ItemRequest> iterator, Set<Location> providers) {
int slot = request.getSlot();
ItemStack prevStack = terminal.getItemInSlot(slot);
if (!(prevStack == null || (prevStack.getAmount() + request.getItem().getAmount() <= prevStack.getMaxStackSize() && SlimefunUtils.isItemSimilar(prevStack, request.getItem(), true, false)))) {
iterator.remove();
return;
}
ItemStack stack = null;
ItemStack item = request.getItem();
for (Location l : providers) {
Optional<Block> target = getAttachedBlock(l.getBlock());
if (target.isPresent()) {
ItemStack is = CargoUtils.withdraw(l.getBlock(), target.get(), item);
if (is != null) {
if (stack == null) {
stack = is;
}
else {
stack = new CustomItem(stack, stack.getAmount() + is.getAmount());
}
if (is.getAmount() == item.getAmount()) {
break;
}
else {
item = new CustomItem(item, item.getAmount() - is.getAmount());
}
}
}
}
if (stack != null) {
ItemStack prev = terminal.getItemInSlot(slot);
if (prev == null) {
terminal.replaceExistingItem(slot, stack);
}
else {
terminal.replaceExistingItem(slot, new CustomItem(stack, stack.getAmount() + prev.getAmount()));
}
}
iterator.remove();
}
private void collectImportRequests() { private void collectImportRequests() {
for (Location bus : imports) { for (Location bus : imports) {
BlockMenu menu = BlockStorage.getInventory(bus); BlockMenu menu = BlockStorage.getInventory(bus);

View File

@ -0,0 +1,13 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.androids;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
@FunctionalInterface
interface AndroidConsumer {
void perform(ProgrammableAndroid android, Block b, BlockMenu inventory, BlockFace face);
}

View File

@ -31,10 +31,11 @@ public abstract class ButcherAndroid extends ProgrammableAndroid {
} }
@Override @Override
protected void killEntities(Block b, double damage, Predicate<Entity> predicate) { protected void attack(Block b, Predicate<LivingEntity> predicate) {
double damage = getTier() >= 3 ? 20D : 4D * getTier();
double radius = 4.0 + getTier(); double radius = 4.0 + getTier();
for (Entity n : b.getWorld().getNearbyEntities(b.getLocation(), radius, radius, radius, n -> n instanceof LivingEntity && !(n instanceof ArmorStand) && !(n instanceof Player) && n.isValid() && predicate.test(n))) { for (Entity n : b.getWorld().getNearbyEntities(b.getLocation(), radius, radius, radius, n -> n instanceof LivingEntity && !(n instanceof ArmorStand) && !(n instanceof Player) && n.isValid() && predicate.test((LivingEntity) n))) {
boolean attack = false; boolean attack = false;
switch (BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation"))) { switch (BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation"))) {

View File

@ -0,0 +1,169 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.androids;
import java.util.function.Predicate;
import org.apache.commons.lang.Validate;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.Animals;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Monster;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
enum Instruction {
// Start and End Parts
START(AndroidType.NONE, "4ae29422db4047efdb9bac2cdae5a0719eb772fccc88a66d912320b343c341"),
REPEAT(AndroidType.NONE, "bc8def67a12622ead1decd3d89364257b531896d87e469813131ca235b5c7"),
WAIT(AndroidType.NONE, "2ee174f41e594e64ea3141c07daf7acf1fa045c230b2b0b0fb3da163db22f455"),
// Movement
GO_FORWARD(AndroidType.NON_FIGHTER, "d9bf6db4aeda9d8822b9f736538e8c18b9a4844f84eb45504adfbfee87eb", (android, b, inv, face) -> {
Block target = b.getRelative(face);
android.move(b, face, target);
}),
GO_UP(AndroidType.NON_FIGHTER, "105a2cab8b68ea57e3af992a36e47c8ff9aa87cc8776281966f8c3cf31a38", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.UP);
android.move(b, face, target);
}),
GO_DOWN(AndroidType.NON_FIGHTER, "c01586e39f6ffa63b4fb301b65ca7da8a92f7353aaab89d3886579125dfbaf9", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.DOWN);
android.move(b, face, target);
}),
// Directions
TURN_LEFT(AndroidType.NONE, "a185c97dbb8353de652698d24b64327b793a3f32a98be67b719fbedab35e", (android, b, inv, face) -> {
int mod = -1;
android.rotate(b, mod);
}),
TURN_RIGHT(AndroidType.NONE, "31c0ededd7115fc1b23d51ce966358b27195daf26ebb6e45a66c34c69c34091", (android, b, inv, face) -> {
int mod = 1;
android.rotate(b, mod);
}),
// Action - Pickaxe
DIG_UP(AndroidType.MINER, "2e6ce011ac9a7a75b2fcd408ad21a3ac1722f6e2eed8781cafd12552282b88", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.UP);
android.dig(b, inv, target);
}),
DIG_FORWARD(AndroidType.MINER, "b6ea2135838461534372f2da6c862d21cd5f3d2c7119f2bb674bbd42791", (android, b, inv, face) -> {
Block target = b.getRelative(face);
android.dig(b, inv, target);
}),
DIG_DOWN(AndroidType.MINER, "8d862024108c785bc0ef7199ec77c402dbbfcc624e9f41f83d8aed8b39fd13", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.DOWN);
android.dig(b, inv, target);
}),
MOVE_AND_DIG_UP(AndroidType.MINER, "2e6ce011ac9a7a75b2fcd408ad21a3ac1722f6e2eed8781cafd12552282b88", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.UP);
android.moveAndDig(b, inv, face, target);
}),
MOVE_AND_DIG_FORWARD(AndroidType.MINER, "b6ea2135838461534372f2da6c862d21cd5f3d2c7119f2bb674bbd42791", (android, b, inv, face) -> {
Block target = b.getRelative(face);
android.moveAndDig(b, inv, face, target);
}),
MOVE_AND_DIG_DOWN(AndroidType.MINER, "8d862024108c785bc0ef7199ec77c402dbbfcc624e9f41f83d8aed8b39fd13", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.DOWN);
android.moveAndDig(b, inv, face, target);
}),
// Action - Sword
ATTACK_MOBS_ANIMALS(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1", (android, b, inv, face) -> {
Predicate<LivingEntity> predicate = e -> true;
android.attack(b, predicate);
}),
ATTACK_MOBS(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1", (android, b, inv, face) -> {
Predicate<LivingEntity> predicate = e -> e instanceof Monster;
android.attack(b, predicate);
}),
ATTACK_ANIMALS(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1", (android, b, inv, face) -> {
Predicate<LivingEntity> predicate = e -> e instanceof Animals;
android.attack(b, predicate);
}),
ATTACK_ANIMALS_ADULT(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1", (android, b, inv, face) -> {
Predicate<LivingEntity> predicate = e -> e instanceof Animals && e instanceof Ageable && ((Ageable) e).isAdult();
android.attack(b, predicate);
}),
// Action - Axe
CHOP_TREE(AndroidType.WOODCUTTER, "64ba49384dba7b7acdb4f70e9361e6d57cbbcbf720cf4f16c2bb83e4557"),
// Action - Fishing Rod
CATCH_FISH(AndroidType.FISHERMAN, "fd4fde511f4454101e4a2a72bc86f12985dfcda76b64bb24dc63a9fa9e3a3", (android, b, inv, face) -> android.fish(b, inv)),
// Action - Hoe
FARM_FORWARD(AndroidType.FARMER, "4de9a522c3d9e7d85f3d82c375dc37fecc856dbd801eb3bcedc1165198bf", (android, b, inv, face) -> {
Block target = b.getRelative(face);
android.farm(inv, target);
}),
FARM_DOWN(AndroidType.FARMER, "2d4296b333d25319af3f33051797f9e6d821cd19a014fb7137beb86a4e9e96", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.DOWN);
android.farm(inv, target);
}),
// Action - ExoticGarden
FARM_EXOTIC_FORWARD(AndroidType.ADVANCED_FARMER, "4de9a522c3d9e7d85f3d82c375dc37fecc856dbd801eb3bcedc1165198bf", (android, b, inv, face) -> {
Block target = b.getRelative(face);
android.exoticFarm(inv, target);
}),
FARM_EXOTIC_DOWN(AndroidType.ADVANCED_FARMER, "2d4296b333d25319af3f33051797f9e6d821cd19a014fb7137beb86a4e9e96", (android, b, inv, face) -> {
Block target = b.getRelative(BlockFace.DOWN);
android.exoticFarm(inv, target);
}),
// Action - Interface
INTERFACE_ITEMS(AndroidType.NONE, "90a4dbf6625c42be57a8ba2c330954a76bdf22785540e87a5c9672685238ec", (android, b, inv, face) -> {
Block target = b.getRelative(face);
android.depositItems(inv, target);
}),
INTERFACE_FUEL(AndroidType.NONE, "2432f5282a50745b912be14deda581bd4a09b977a3c32d7e9578491fee8fa7", (android, b, inv, face) -> {
Block target = b.getRelative(face);
android.refuel(inv, target);
});
private final ItemStack item;
private final AndroidType type;
private final AndroidConsumer method;
Instruction(AndroidType type, String texture, AndroidConsumer method) {
this.type = type;
this.item = SlimefunUtils.getCustomHead(texture);
this.method = method;
}
Instruction(AndroidType type, String texture) {
this(type, texture, null);
}
public ItemStack getItem() {
return item;
}
public AndroidType getRequiredType() {
return type;
}
public void execute(ProgrammableAndroid android, Block b, BlockMenu inventory, BlockFace face) {
Validate.notNull(method, "This Instruction must be executed manually!");
method.perform(android, b, inventory, face);
}
}

View File

@ -36,7 +36,7 @@ public abstract class MinerAndroid extends ProgrammableAndroid {
} }
@Override @Override
protected void mine(Block b, BlockMenu menu, Block block) { protected void dig(Block b, BlockMenu menu, Block block) {
Collection<ItemStack> drops = block.getDrops(effectivePickaxe); Collection<ItemStack> drops = block.getDrops(effectivePickaxe);
if (!MaterialCollections.getAllUnbreakableBlocks().contains(block.getType()) && !drops.isEmpty() && SlimefunPlugin.getProtectionManager().hasPermission(Bukkit.getOfflinePlayer(UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner"))), block.getLocation(), ProtectableAction.BREAK_BLOCK)) { if (!MaterialCollections.getAllUnbreakableBlocks().contains(block.getType()) && !drops.isEmpty() && SlimefunPlugin.getProtectionManager().hasPermission(Bukkit.getOfflinePlayer(UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner"))), block.getLocation(), ProtectableAction.BREAK_BLOCK)) {
@ -63,7 +63,7 @@ public abstract class MinerAndroid extends ProgrammableAndroid {
} }
@Override @Override
protected void movedig(Block b, BlockMenu menu, BlockFace face, Block block) { protected void moveAndDig(Block b, BlockMenu menu, BlockFace face, Block block) {
Collection<ItemStack> drops = block.getDrops(effectivePickaxe); Collection<ItemStack> drops = block.getDrops(effectivePickaxe);
if (!MaterialCollections.getAllUnbreakableBlocks().contains(block.getType()) && !drops.isEmpty() && SlimefunPlugin.getProtectionManager().hasPermission(Bukkit.getOfflinePlayer(UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner"))), block.getLocation(), ProtectableAction.BREAK_BLOCK)) { if (!MaterialCollections.getAllUnbreakableBlocks().contains(block.getType()) && !drops.isEmpty() && SlimefunPlugin.getProtectionManager().hasPermission(Bukkit.getOfflinePlayer(UUID.fromString(BlockStorage.getLocationInfo(b.getLocation(), "owner"))), block.getLocation(), ProtectableAction.BREAK_BLOCK)) {

View File

@ -22,9 +22,7 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.Dispenser; import org.bukkit.block.Dispenser;
import org.bukkit.block.data.Rotatable; import org.bukkit.block.data.Rotatable;
import org.bukkit.entity.Animals; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Monster;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
@ -200,7 +198,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
public void openScript(Player p, Block b, String script) { public void openScript(Player p, Block b, String script) {
ChestMenu menu = new ChestMenu(ChatColor.DARK_AQUA + SlimefunPlugin.getLocal().getMessage(p, "android.scripts.editor")); ChestMenu menu = new ChestMenu(ChatColor.DARK_AQUA + SlimefunPlugin.getLocal().getMessage(p, "android.scripts.editor"));
menu.addItem(0, new CustomItem(ScriptAction.START.getItem(), SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions.START"), "", "&7\u21E8 &eLeft Click &7to return to the Android's interface")); menu.addItem(0, new CustomItem(Instruction.START.getItem(), SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions.START"), "", "&7\u21E8 &eLeft Click &7to return to the Android's interface"));
menu.addMenuClickHandler(0, (pl, slot, item, action) -> { menu.addMenuClickHandler(0, (pl, slot, item, action) -> {
BlockStorage.getInventory(b).open(pl); BlockStorage.getInventory(b).open(pl);
return false; return false;
@ -222,21 +220,21 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
}); });
} }
menu.addItem(i + additional, new CustomItem(ScriptAction.REPEAT.getItem(), SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions.REPEAT"), "", "&7\u21E8 &eLeft Click &7to return to the Android's interface")); menu.addItem(i + additional, new CustomItem(Instruction.REPEAT.getItem(), SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions.REPEAT"), "", "&7\u21E8 &eLeft Click &7to return to the Android's interface"));
menu.addMenuClickHandler(i + additional, (pl, slot, item, action) -> { menu.addMenuClickHandler(i + additional, (pl, slot, item, action) -> {
BlockStorage.getInventory(b).open(pl); BlockStorage.getInventory(b).open(pl);
return false; return false;
}); });
} }
else { else {
ItemStack stack = ScriptAction.valueOf(commands[i]).getItem(); ItemStack stack = Instruction.valueOf(commands[i]).getItem();
menu.addItem(i, new CustomItem(stack, SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions." + ScriptAction.valueOf(commands[i]).name()), "", "&7\u21E8 &eLeft Click &7to edit", "&7\u21E8 &eRight Click &7to delete", "&7\u21E8 &eShift + Right Click &7to duplicate")); menu.addItem(i, new CustomItem(stack, SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions." + Instruction.valueOf(commands[i]).name()), "", "&7\u21E8 &eLeft Click &7to edit", "&7\u21E8 &eRight Click &7to delete", "&7\u21E8 &eShift + Right Click &7to duplicate"));
menu.addMenuClickHandler(i, (pl, slot, item, action) -> { menu.addMenuClickHandler(i, (pl, slot, item, action) -> {
if (action.isRightClicked() && action.isShiftClicked()) { if (action.isRightClicked() && action.isShiftClicked()) {
if (commands.length == 54) return false; if (commands.length == 54) return false;
int j = 0; int j = 0;
StringBuilder builder = new StringBuilder(ScriptAction.START + "-"); StringBuilder builder = new StringBuilder(Instruction.START + "-");
for (String command : commands) { for (String command : commands) {
if (j > 0) { if (j > 0) {
@ -247,20 +245,20 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
} }
j++; j++;
} }
builder.append(ScriptAction.REPEAT); builder.append(Instruction.REPEAT);
setScript(b.getLocation(), builder.toString()); setScript(b.getLocation(), builder.toString());
openScript(pl, b, builder.toString()); openScript(pl, b, builder.toString());
} }
else if (action.isRightClicked()) { else if (action.isRightClicked()) {
int j = 0; int j = 0;
StringBuilder builder = new StringBuilder(ScriptAction.START + "-"); StringBuilder builder = new StringBuilder(Instruction.START + "-");
for (String command : commands) { for (String command : commands) {
if (j != index && j > 0 && j < commands.length - 1) builder.append(command).append('-'); if (j != index && j > 0 && j < commands.length - 1) builder.append(command).append('-');
j++; j++;
} }
builder.append(ScriptAction.REPEAT); builder.append(Instruction.REPEAT);
setScript(b.getLocation(), builder.toString()); setScript(b.getLocation(), builder.toString());
openScript(pl, b, builder.toString()); openScript(pl, b, builder.toString());
@ -337,10 +335,10 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
String author = (op != null && op.getName() != null) ? op.getName() : script.getString("author_name"); String author = (op != null && op.getName() != null) ? op.getName() : script.getString("author_name");
if (script.getString("author").equals(p.getUniqueId().toString())) { if (script.getString("author").equals(p.getUniqueId().toString())) {
menu.addItem(index, new CustomItem(this.getItem(), "&b" + script.getString("name"), "&7by &r" + author, "", "&7Downloads: &r" + script.getInt("downloads"), "&7Rating: " + getScriptRatingPercentage(script), "&a" + getScriptRating(script, true) + " \u263A &7| &4\u2639 " + getScriptRating(script, false), "", "&eLeft Click &rto download this Script", "&4(This will override your current Script)")); menu.addItem(index, new CustomItem(getItem(), "&b" + script.getString("name"), "&7by &r" + author, "", "&7Downloads: &r" + script.getInt("downloads"), "&7Rating: " + getScriptRatingPercentage(script), "&a" + getScriptRating(script, true) + " \u263A &7| &4\u2639 " + getScriptRating(script, false), "", "&eLeft Click &rto download this Script", "&4(This will override your current Script)"));
} }
else { else {
menu.addItem(index, new CustomItem(this.getItem(), "&b" + script.getString("name"), "&7by &r" + author, "", "&7Downloads: &r" + script.getInt("downloads"), "&7Rating: " + getScriptRatingPercentage(script), "&a" + getScriptRating(script, true) + " \u263A &7| &4\u2639 " + getScriptRating(script, false), "", "&eLeft Click &rto download this Script", "&4(This will override your current Script)", "&eShift + Left Click &rto leave a positive Rating", "&eShift + Right Click &rto leave a negative Rating")); menu.addItem(index, new CustomItem(getItem(), "&b" + script.getString("name"), "&7by &r" + author, "", "&7Downloads: &r" + script.getInt("downloads"), "&7Rating: " + getScriptRatingPercentage(script), "&a" + getScriptRating(script, true) + " \u263A &7| &4\u2639 " + getScriptRating(script, false), "", "&eLeft Click &rto download this Script", "&4(This will override your current Script)", "&eShift + Left Click &rto leave a positive Rating", "&eShift + Right Click &rto leave a negative Rating"));
} }
menu.addMenuClickHandler(index, (pl, slot, item, action) -> { menu.addMenuClickHandler(index, (pl, slot, item, action) -> {
@ -481,7 +479,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
if (!mainDirectory.exists()) mainDirectory.mkdirs(); if (!mainDirectory.exists()) mainDirectory.mkdirs();
for (File script : mainDirectory.listFiles()) { for (File script : mainDirectory.listFiles()) {
if (script.getName().endsWith("sfs")) { if (script.getName().endsWith(".sfs")) {
scripts.add(new Config(script)); scripts.add(new Config(script));
} }
} }
@ -492,11 +490,11 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
return scripts; return scripts;
} }
protected List<ScriptAction> getAccessibleScriptParts() { protected List<Instruction> getAccessibleScriptParts() {
List<ScriptAction> list = new ArrayList<>(); List<Instruction> list = new ArrayList<>();
for (ScriptAction part : ScriptAction.values()) { for (Instruction part : Instruction.values()) {
if (part != ScriptAction.START && part != ScriptAction.REPEAT && getAndroidType().isType(part.getRequiredType())) { if (part != Instruction.START && part != Instruction.REPEAT && getAndroidType().isType(part.getRequiredType())) {
list.add(part); list.add(part);
} }
} }
@ -516,15 +514,8 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
} }
protected String getScriptRatingPercentage(Config script) { protected String getScriptRatingPercentage(Config script) {
String progress = String.valueOf(getScriptRating(script)); float percentage = getScriptRating(script);
if (Float.parseFloat(progress) < 16.0F) progress = "&4" + progress + "&r% "; return NumberUtils.getColorFromPercentage(percentage) + String.valueOf(percentage) + ChatColor.RESET + "% ";
else if (Float.parseFloat(progress) < 32.0F) progress = "&c" + progress + "&r% ";
else if (Float.parseFloat(progress) < 48.0F) progress = "&6" + progress + "&r% ";
else if (Float.parseFloat(progress) < 64.0F) progress = "&e" + progress + "&r% ";
else if (Float.parseFloat(progress) < 80.0F) progress = "&2" + progress + "&r% ";
else progress = "&a" + progress + "&r% ";
return progress;
} }
protected void openScriptComponentEditor(Player p, Block b, String script, int index) { protected void openScriptComponentEditor(Player p, Block b, String script, int index) {
@ -550,7 +541,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
}); });
int i = 10; int i = 10;
for (ScriptAction part : getAccessibleScriptParts()) { for (Instruction part : getAccessibleScriptParts()) {
menu.addItem(i, new CustomItem(part.getItem(), SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions." + part.name())), (pl, slot, item, action) -> { menu.addItem(i, new CustomItem(part.getItem(), SlimefunPlugin.getLocal().getMessage(p, "android.scripts.instructions." + part.name())), (pl, slot, item, action) -> {
addInstruction(pl, b, index, part, commands); addInstruction(pl, b, index, part, commands);
return false; return false;
@ -561,7 +552,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
menu.open(p); menu.open(p);
} }
private void addInstruction(Player p, Block b, int index, ScriptAction part, String[] commands) { private void addInstruction(Player p, Block b, int index, Instruction part, String[] commands) {
int j = 0; int j = 0;
StringBuilder builder = new StringBuilder("START-"); StringBuilder builder = new StringBuilder("START-");
@ -671,107 +662,30 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
String[] script = PatternUtils.DASH.split(BlockStorage.getLocationInfo(b.getLocation(), "script")); String[] script = PatternUtils.DASH.split(BlockStorage.getLocationInfo(b.getLocation(), "script"));
int index = Integer.parseInt(BlockStorage.getLocationInfo(b.getLocation(), "index")) + 1; int index = Integer.parseInt(BlockStorage.getLocationInfo(b.getLocation(), "index")) + 1;
if (index >= script.length) index = 0; if (index >= script.length) {
index = 0;
}
boolean refresh = true; boolean refresh = true;
BlockStorage.addBlockInfo(b, "fuel", String.valueOf(fuel - 1)); BlockStorage.addBlockInfo(b, "fuel", String.valueOf(fuel - 1));
ScriptAction part = ScriptAction.valueOf(script[index]); Instruction instruction = Instruction.valueOf(script[index]);
if (getAndroidType().isType(part.getRequiredType())) { if (getAndroidType().isType(instruction.getRequiredType())) {
BlockFace face = BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation")); BlockFace face = BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation"));
double damage = getTier() >= 3 ? 20D : 4D * getTier();
switch (part) { switch (instruction) {
case GO_DOWN:
move(b, face, b.getRelative(BlockFace.DOWN));
break;
case GO_FORWARD:
move(b, face, b.getRelative(face));
break;
case GO_UP:
move(b, face, b.getRelative(BlockFace.UP));
break;
case REPEAT: case REPEAT:
BlockStorage.addBlockInfo(b, "index", String.valueOf(0)); BlockStorage.addBlockInfo(b, "index", String.valueOf(0));
break; break;
case TURN_LEFT:
int indexLeft = POSSIBLE_ROTATIONS.indexOf(BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation"))) - 1;
if (indexLeft < 0) indexLeft = POSSIBLE_ROTATIONS.size() - 1;
Rotatable rotatableLeft = (Rotatable) b.getBlockData();
rotatableLeft.setRotation(POSSIBLE_ROTATIONS.get(indexLeft));
b.setBlockData(rotatableLeft);
BlockStorage.addBlockInfo(b, "rotation", POSSIBLE_ROTATIONS.get(indexLeft).toString());
break;
case TURN_RIGHT:
int indexRight = POSSIBLE_ROTATIONS.indexOf(BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation"))) + 1;
if (indexRight == POSSIBLE_ROTATIONS.size()) indexRight = 0;
Rotatable rotatableRight = (Rotatable) b.getBlockData();
rotatableRight.setRotation(POSSIBLE_ROTATIONS.get(indexRight));
b.setBlockData(rotatableRight);
BlockStorage.addBlockInfo(b, "rotation", POSSIBLE_ROTATIONS.get(indexRight).toString());
break;
case DIG_FORWARD:
mine(b, menu, b.getRelative(face));
break;
case DIG_UP:
mine(b, menu, b.getRelative(BlockFace.UP));
break;
case DIG_DOWN:
mine(b, menu, b.getRelative(BlockFace.DOWN));
break;
case CATCH_FISH:
fish(b, menu);
break;
case MOVE_AND_DIG_FORWARD:
movedig(b, menu, face, b.getRelative(face));
break;
case MOVE_AND_DIG_UP:
movedig(b, menu, face, b.getRelative(BlockFace.UP));
break;
case MOVE_AND_DIG_DOWN:
movedig(b, menu, face, b.getRelative(BlockFace.DOWN));
break;
case INTERFACE_ITEMS:
depositItems(menu, b.getRelative(face));
break;
case INTERFACE_FUEL:
refuel(menu, b.getRelative(face));
break;
case FARM_FORWARD:
farm(menu, b.getRelative(face));
break;
case FARM_DOWN:
farm(menu, b.getRelative(BlockFace.DOWN));
break;
case FARM_EXOTIC_FORWARD:
exoticFarm(menu, b.getRelative(face));
break;
case FARM_EXOTIC_DOWN:
exoticFarm(menu, b.getRelative(BlockFace.DOWN));
break;
case CHOP_TREE: case CHOP_TREE:
refresh = chopTree(b, menu, face); refresh = chopTree(b, menu, face);
break; break;
case ATTACK_MOBS_ANIMALS:
killEntities(b, damage, e -> true);
break;
case ATTACK_MOBS:
killEntities(b, damage, e -> e instanceof Monster);
break;
case ATTACK_ANIMALS:
killEntities(b, damage, e -> e instanceof Animals);
break;
case ATTACK_ANIMALS_ADULT:
killEntities(b, damage, e -> e instanceof Animals && e instanceof org.bukkit.entity.Ageable && ((org.bukkit.entity.Ageable) e).isAdult());
break;
default: default:
instruction.execute(this, b, menu, face);
break; break;
} }
} }
if (refresh) { if (refresh) {
BlockStorage.addBlockInfo(b, "index", String.valueOf(index)); BlockStorage.addBlockInfo(b, "index", String.valueOf(index));
} }
@ -779,7 +693,26 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
} }
} }
private void depositItems(BlockMenu menu, Block facedBlock) { protected void rotate(Block b, int mod) {
BlockFace current = BlockFace.valueOf(BlockStorage.getLocationInfo(b.getLocation(), "rotation"));
int index = POSSIBLE_ROTATIONS.indexOf(current) + mod;
if (index == POSSIBLE_ROTATIONS.size()) {
index = 0;
}
else if (index < 0) {
index = POSSIBLE_ROTATIONS.size() - 1;
}
BlockFace rotation = POSSIBLE_ROTATIONS.get(index);
Rotatable rotatatable = (Rotatable) b.getBlockData();
rotatatable.setRotation(rotation);
b.setBlockData(rotatatable);
BlockStorage.addBlockInfo(b, "rotation", rotation.name());
}
protected void depositItems(BlockMenu menu, Block facedBlock) {
if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_ITEMS")) { if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_ITEMS")) {
Dispenser d = (Dispenser) facedBlock.getState(); Dispenser d = (Dispenser) facedBlock.getState();
@ -800,26 +733,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
} }
} }
private void consumeFuel(Block b, BlockMenu menu) { protected void refuel(BlockMenu menu, Block facedBlock) {
ItemStack item = menu.getItemInSlot(43);
if (item != null) {
for (MachineFuel fuel : fuelTypes) {
if (fuel.test(item)) {
menu.consumeItem(43);
if (getTier() == 2) {
menu.pushItem(new ItemStack(Material.BUCKET), getOutputSlots());
}
BlockStorage.addBlockInfo(b, "fuel", String.valueOf((int) (fuel.getTicks() * this.getFuelEfficiency())));
break;
}
}
}
}
private void refuel(BlockMenu menu, Block facedBlock) {
if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_FUEL")) { if (facedBlock.getType() == Material.DISPENSER && BlockStorage.check(facedBlock, "ANDROID_INTERFACE_FUEL")) {
Dispenser d = (Dispenser) facedBlock.getState(); Dispenser d = (Dispenser) facedBlock.getState();
@ -854,6 +768,25 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
return false; return false;
} }
private void consumeFuel(Block b, BlockMenu menu) {
ItemStack item = menu.getItemInSlot(43);
if (item != null) {
for (MachineFuel fuel : fuelTypes) {
if (fuel.test(item)) {
menu.consumeItem(43);
if (getTier() == 2) {
menu.pushItem(new ItemStack(Material.BUCKET), getOutputSlots());
}
BlockStorage.addBlockInfo(b, "fuel", String.valueOf((int) (fuel.getTicks() * this.getFuelEfficiency())));
break;
}
}
}
}
private void constructMenu(BlockMenuPreset preset) { private void constructMenu(BlockMenuPreset preset) {
for (int i : BORDER) { for (int i : BORDER) {
preset.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " "), ChestMenuUtils.getEmptyClickHandler()); preset.addItem(i, new CustomItem(new ItemStack(Material.GRAY_STAINED_GLASS_PANE), " "), ChestMenuUtils.getEmptyClickHandler());
@ -916,7 +849,7 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
} }
} }
protected void killEntities(Block b, double damage, Predicate<Entity> predicate) { protected void attack(Block b, Predicate<LivingEntity> predicate) {
throw new UnsupportedOperationException("Non-butcher Android tried to butcher!"); throw new UnsupportedOperationException("Non-butcher Android tried to butcher!");
} }
@ -924,11 +857,11 @@ public abstract class ProgrammableAndroid extends SlimefunItem implements Invent
throw new UnsupportedOperationException("Non-fishing Android tried to fish!"); throw new UnsupportedOperationException("Non-fishing Android tried to fish!");
} }
protected void mine(Block b, BlockMenu menu, Block block) { protected void dig(Block b, BlockMenu menu, Block block) {
throw new UnsupportedOperationException("Non-mining Android tried to mine!"); throw new UnsupportedOperationException("Non-mining Android tried to mine!");
} }
protected void movedig(Block b, BlockMenu menu, BlockFace face, Block block) { protected void moveAndDig(Block b, BlockMenu menu, BlockFace face, Block block) {
throw new UnsupportedOperationException("Non-mining Android tried to mine!"); throw new UnsupportedOperationException("Non-mining Android tried to mine!");
} }

View File

@ -1,72 +0,0 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.androids;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
enum ScriptAction {
// Start and End Parts
START(AndroidType.NONE, "4ae29422db4047efdb9bac2cdae5a0719eb772fccc88a66d912320b343c341"),
REPEAT(AndroidType.NONE, "bc8def67a12622ead1decd3d89364257b531896d87e469813131ca235b5c7"),
WAIT(AndroidType.NONE, "2ee174f41e594e64ea3141c07daf7acf1fa045c230b2b0b0fb3da163db22f455"),
// Movement
GO_FORWARD(AndroidType.NON_FIGHTER, "d9bf6db4aeda9d8822b9f736538e8c18b9a4844f84eb45504adfbfee87eb"),
GO_UP(AndroidType.NON_FIGHTER, "105a2cab8b68ea57e3af992a36e47c8ff9aa87cc8776281966f8c3cf31a38"),
GO_DOWN(AndroidType.NON_FIGHTER, "c01586e39f6ffa63b4fb301b65ca7da8a92f7353aaab89d3886579125dfbaf9"),
// Directions
TURN_LEFT(AndroidType.NONE, "a185c97dbb8353de652698d24b64327b793a3f32a98be67b719fbedab35e"),
TURN_RIGHT(AndroidType.NONE, "31c0ededd7115fc1b23d51ce966358b27195daf26ebb6e45a66c34c69c34091"),
// Action - Pickaxe
DIG_UP(AndroidType.MINER, "2e6ce011ac9a7a75b2fcd408ad21a3ac1722f6e2eed8781cafd12552282b88"),
DIG_FORWARD(AndroidType.MINER, "b6ea2135838461534372f2da6c862d21cd5f3d2c7119f2bb674bbd42791"),
DIG_DOWN(AndroidType.MINER, "8d862024108c785bc0ef7199ec77c402dbbfcc624e9f41f83d8aed8b39fd13"),
MOVE_AND_DIG_UP(AndroidType.MINER, "2e6ce011ac9a7a75b2fcd408ad21a3ac1722f6e2eed8781cafd12552282b88"),
MOVE_AND_DIG_FORWARD(AndroidType.MINER, "b6ea2135838461534372f2da6c862d21cd5f3d2c7119f2bb674bbd42791"),
MOVE_AND_DIG_DOWN(AndroidType.MINER, "8d862024108c785bc0ef7199ec77c402dbbfcc624e9f41f83d8aed8b39fd13"),
// Action - Sword
ATTACK_MOBS_ANIMALS(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1"),
ATTACK_MOBS(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1"),
ATTACK_ANIMALS(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1"),
ATTACK_ANIMALS_ADULT(AndroidType.FIGHTER, "c7e6c40f68b775f2efcd7bd9916b327869dcf27e24c855d0a18e07ac04fe1"),
// Action - Axe
CHOP_TREE(AndroidType.WOODCUTTER, "64ba49384dba7b7acdb4f70e9361e6d57cbbcbf720cf4f16c2bb83e4557"),
// Action - Fishing Rod
CATCH_FISH(AndroidType.FISHERMAN, "fd4fde511f4454101e4a2a72bc86f12985dfcda76b64bb24dc63a9fa9e3a3"),
// Action - Hoe
FARM_FORWARD(AndroidType.FARMER, "4de9a522c3d9e7d85f3d82c375dc37fecc856dbd801eb3bcedc1165198bf"),
FARM_DOWN(AndroidType.FARMER, "2d4296b333d25319af3f33051797f9e6d821cd19a014fb7137beb86a4e9e96"),
// Action - ExoticGarden
FARM_EXOTIC_FORWARD(AndroidType.ADVANCED_FARMER, "4de9a522c3d9e7d85f3d82c375dc37fecc856dbd801eb3bcedc1165198bf"),
FARM_EXOTIC_DOWN(AndroidType.ADVANCED_FARMER, "2d4296b333d25319af3f33051797f9e6d821cd19a014fb7137beb86a4e9e96"),
// Action - Interface
INTERFACE_ITEMS(AndroidType.NONE, "90a4dbf6625c42be57a8ba2c330954a76bdf22785540e87a5c9672685238ec"),
INTERFACE_FUEL(AndroidType.NONE, "2432f5282a50745b912be14deda581bd4a09b977a3c32d7e9578491fee8fa7");
private final ItemStack item;
private final AndroidType type;
ScriptAction(AndroidType type, String texture) {
this.type = type;
this.item = SlimefunUtils.getCustomHead(texture);
}
public ItemStack getItem() {
return item;
}
public AndroidType getRequiredType() {
return type;
}
}