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

Added a Tape Measure to the experimental branch

This commit is contained in:
TheBusyBiscuit 2020-07-13 11:55:59 +02:00
parent f5db4bf695
commit 50c579b7d6
5 changed files with 131 additions and 1 deletions

View File

@ -26,6 +26,7 @@
* Added Reinforced Cloth
* Added Bee protection to Hazmat Suit
* Added Enchantment Rune
* Added Tape Measure
## Release Candidate 14 (12 Jul 2020)

View File

@ -44,7 +44,8 @@ public final class SlimefunItems {
public static final SlimefunItemStack BROKEN_SPAWNER = new SlimefunItemStack("BROKEN_SPAWNER", Material.SPAWNER, "&cBroken Spawner", "&7Type: &b<Type>", "", "&cFractured, must be repaired in an Ancient Altar");
public static final SlimefunItemStack REPAIRED_SPAWNER = new SlimefunItemStack("REINFORCED_SPAWNER", Material.SPAWNER, "&bReinforced Spawner", "&7Type: &b<Type>");
public static final SlimefunItemStack INFERNAL_BONEMEAL = new SlimefunItemStack("INFERNAL_BONEMEAL", Material.BONE_MEAL, "&4Infernal Bonemeal", "", "&cSpeeds up the Growth of", "&cNether Warts as well");
public static final SlimefunItemStack TAPE_MEASURE = new SlimefunItemStack("TAPE_MEASURE", "180d5c43a6cf5bb7769fd0c8240e1e70d2ae38ef9d78a1db401aca6a2cb36f65", "&6Tape Measure", "", "&eCrouch & Right Click &7to set an anchor", "&eRight Click &7to measure");
/* Gadgets */
public static final SlimefunItemStack GOLD_PAN = new SlimefunItemStack("GOLD_PAN", Material.BOWL, "&6Gold Pan", "", "&eRight Click&7 to collect resources", "&7from Gravel");
public static final SlimefunItemStack NETHER_GOLD_PAN = new SlimefunItemStack("NETHER_GOLD_PAN", Material.BOWL, "&4Nether Gold Pan", "", "&eRight Click&7 to collect resources", "&7from Soul Sand");

View File

@ -0,0 +1,115 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
import java.text.DecimalFormat;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable;
import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* The {@link TapeMeasure} is used to measure the distance between two {@link Block Blocks}.
*
* @author TheBusyBiscuit
*
*/
public class TapeMeasure extends SimpleSlimefunItem<ItemUseHandler> implements NotPlaceable {
private final NamespacedKey key = new NamespacedKey(SlimefunPlugin.instance(), "anchor");
private final DecimalFormat format = new DecimalFormat("##.###");
public TapeMeasure(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
}
@Override
public ItemUseHandler getItemHandler() {
return e -> {
e.cancel();
if (e.getClickedBlock().isPresent()) {
Block block = e.getClickedBlock().get();
if (e.getPlayer().isSneaking()) {
setAnchor(e.getPlayer(), e.getItem(), block);
}
else {
measure(e.getPlayer(), e.getItem(), block);
}
}
};
}
private void setAnchor(Player p, ItemStack item, Block block) {
ItemMeta meta = item.getItemMeta();
JsonObject json = new JsonObject();
json.addProperty("world", block.getWorld().getUID().toString());
json.addProperty("x", block.getX());
json.addProperty("y", block.getY());
json.addProperty("z", block.getZ());
meta.getPersistentDataContainer().set(key, PersistentDataType.STRING, json.toString());
String anchor = block.getX() + " | " + block.getY() + " | " + block.getZ();
SlimefunPlugin.getLocalization().sendMessage(p, "messages.tape-measure.anchor-set", msg -> msg.replace("%anchor%", anchor));
item.setItemMeta(meta);
}
private Optional<Location> getAnchor(Player p, ItemStack item) {
ItemMeta meta = item.getItemMeta();
String data = meta.getPersistentDataContainer().get(key, PersistentDataType.STRING);
if (data != null) {
JsonObject json = new JsonParser().parse(data).getAsJsonObject();
UUID uuid = UUID.fromString(json.get("world").getAsString());
if (p.getWorld().getUID().equals(uuid)) {
int x = json.get("x").getAsInt();
int y = json.get("y").getAsInt();
int z = json.get("z").getAsInt();
Location loc = new Location(p.getWorld(), x, y, z);
return Optional.of(loc);
}
else {
SlimefunPlugin.getLocalization().sendMessage(p, "messages.tape-measure.wrong-world");
return Optional.empty();
}
}
else {
SlimefunPlugin.getLocalization().sendMessage(p, "messages.tape-measure.no-anchor");
return Optional.empty();
}
}
private void measure(Player p, ItemStack item, Block block) {
Optional<Location> anchor = getAnchor(p, item);
if (anchor.isPresent()) {
Location loc = anchor.get();
double distance = loc.distance(block.getLocation());
SlimefunPlugin.getLocalization().sendMessage(p, "messages.tape-measure.distance", msg -> msg.replace("%distance%", format.format(distance)));
}
}
}

View File

@ -173,6 +173,7 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.tools.PickaxeOfVe
import io.github.thebusybiscuit.slimefun4.implementation.items.tools.PortableCrafter;
import io.github.thebusybiscuit.slimefun4.implementation.items.tools.PortableDustbin;
import io.github.thebusybiscuit.slimefun4.implementation.items.tools.SmeltersPickaxe;
import io.github.thebusybiscuit.slimefun4.implementation.items.tools.TapeMeasure;
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.ExplosiveBow;
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.IcyBow;
import io.github.thebusybiscuit.slimefun4.implementation.items.weapons.SeismicAxe;
@ -3253,6 +3254,12 @@ public final class SlimefunItemSetup {
new WitherAssembler(categories.electricity, SlimefunItems.WITHER_ASSEMBLER, RecipeType.ENHANCED_CRAFTING_TABLE,
new ItemStack[] {SlimefunItems.BLISTERING_INGOT_3, new ItemStack(Material.NETHER_STAR), SlimefunItems.BLISTERING_INGOT_3, SlimefunItems.WITHER_PROOF_OBSIDIAN, SlimefunItems.ANDROID_MEMORY_CORE, SlimefunItems.WITHER_PROOF_OBSIDIAN, SlimefunItems.ELECTRIC_MOTOR, SlimefunItems.REINFORCED_ALLOY_INGOT, SlimefunItems.CARBONADO_EDGED_CAPACITOR})
.register(plugin);
if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
new TapeMeasure(categories.usefulItems, SlimefunItems.TAPE_MEASURE, RecipeType.ENHANCED_CRAFTING_TABLE,
new ItemStack[] {SlimefunItems.SILICON, new ItemStack(Material.YELLOW_DYE), SlimefunItems.SILICON, new ItemStack(Material.YELLOW_DYE), new ItemStack(Material.STRING), new ItemStack(Material.YELLOW_DYE), SlimefunItems.GILDED_IRON, new ItemStack(Material.YELLOW_DYE), SlimefunItems.SILICON})
.register(plugin);
}
}
private static void registerArmorSet(Category category, ItemStack baseComponent, ItemStack[] items, String idSyntax, boolean vanilla, SlimefunAddon addon) {

View File

@ -138,6 +138,12 @@ messages:
research:
start: '&7The Ancient Spirits whisper mysterious words into your ear!'
progress: '&7You start to wonder about &b%research% &e(%progress%)'
tape-measure:
no-anchor: '&cYou need to set an anchor before you can start to measure!'
wrong-world: '&cYour anchor seems to be in a different world!'
distance: '&7Measurement taken. &eDistance: %distance%'
anchor-set: '&aSuccessfully set the anchor:&e %anchor%'
fire-extinguish: '&7You have extinguished yourself'
cannot-place: '&cYou cannot place that block there!'