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

Fixed Output Chest

This commit is contained in:
TheBusyBiscuit 2019-12-04 00:04:34 +01:00
parent 0fd7892ca4
commit da1dd294f6
6 changed files with 178 additions and 4 deletions

View File

@ -14,8 +14,12 @@ public abstract class JsonDataHolder {
protected JsonObject data;
private boolean dirty;
public JsonDataHolder() {
this(null);
}
public JsonDataHolder(JsonObject data) {
this.data = data;
this.data = data != null ? data: new JsonObject();
this.dirty = false;
}

View File

@ -1,12 +1,17 @@
package io.github.thebusybiscuit.slimefun4.api;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.blocks.SlimefunBlock;
import io.github.thebusybiscuit.slimefun4.api.items.ItemRestriction;
import io.github.thebusybiscuit.slimefun4.core.SlimefunWorld;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.PlayerProfile;
@ -84,4 +89,12 @@ public interface SlimefunAPI {
return getCategories().stream().flatMap(cat -> cat.getItems().stream()).collect(Collectors.toSet());
}
Optional<SlimefunItem> fromItemStack(ItemStack item);
Optional<SlimefunBlock> fromBlock();
SlimefunWorld fromWorld(World world);
PlayerProfile fromPlayer(OfflinePlayer player);
}

View File

@ -0,0 +1,59 @@
package io.github.thebusybiscuit.slimefun4.api.blocks;
import java.util.Objects;
import org.bukkit.Location;
import org.bukkit.block.Block;
public final class BlockLocation {
private final int x;
private final int y;
private final int z;
public BlockLocation(Block b) {
this(b.getX(), b.getY(), b.getZ());
}
public BlockLocation(Location l) {
this(l.getBlockX(), l.getBlockY(), l.getBlockZ());
}
public BlockLocation(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BlockLocation) {
BlockLocation l = (BlockLocation) obj;
return l.getX() == x && l.getY() == y && l.getZ() == z;
}
else return false;
}
@Override
public int hashCode() {
return Objects.hash(x, y, z);
}
@Override
public String toString() {
return "[" + x + " | " + y + " | " + z + "]";
}
}

View File

@ -0,0 +1,25 @@
package io.github.thebusybiscuit.slimefun4.api.blocks;
import com.google.gson.JsonObject;
import io.github.thebusybiscuit.slimefun4.api.JsonDataHolder;
public class SlimefunBlock extends JsonDataHolder {
private String id;
public SlimefunBlock(String id) {
this(id, null);
}
public SlimefunBlock(String id, JsonObject data) {
super(data);
this.id = id;
}
public String getID() {
return id;
}
}

View File

@ -0,0 +1,64 @@
package io.github.thebusybiscuit.slimefun4.core;
import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.TileState;
import io.github.thebusybiscuit.cscorelib2.collections.OptionalMap;
import io.github.thebusybiscuit.slimefun4.api.blocks.BlockLocation;
import io.github.thebusybiscuit.slimefun4.api.blocks.SlimefunBlock;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
public class SlimefunWorld {
private final UUID uuid;
private final OptionalMap<BlockLocation, SlimefunBlock> blocks = new OptionalMap<>(HashMap::new);
public SlimefunWorld(UUID uuid) {
this.uuid = uuid;
}
/**
* This method will return the {@link UUID} of this instance.
* The {@link UUID} will be the same for {@link SlimefunWorld} and the corresponding instance
* of {@link World}.
*
* @return This world's {@link UUID}
*/
public UUID getUUID() {
return uuid;
}
/**
* This method will reliably get the instance of {@link SlimefunBlock} associated with the given
* {@link Block}.
*
* @param b The {@link Block} to query
* @return An {@link Optional} of the requested {@link SlimefunBlock}, empty if none was found
*/
public Optional<SlimefunBlock> getBlock(Block b) {
if (b.getState() instanceof TileState) {
Optional<String> blockData = SlimefunPlugin.getBlockDataService().getBlockData((TileState) b.getState());
if (blockData.isPresent()) {
return Optional.of(new SlimefunBlock(blockData.get()));
}
}
return blocks.get(new BlockLocation(b));
}
public boolean isBlock(Block b, String id) {
if (id == null) {
throw new IllegalArgumentException("Cannot check blocks for id: null");
}
Optional<SlimefunBlock> block = getBlock(b);
return block.isPresent() && block.get().getID().equals(id);
}
}

View File

@ -8,6 +8,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@ -17,6 +18,7 @@ import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.TileState;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.HumanEntity;
@ -561,12 +563,19 @@ public class BlockStorage {
return SlimefunItem.getByID(getLocationInfo(l, "id"));
}
public static String checkID(Block block) {
return checkID(block.getLocation());
public static String checkID(Block b) {
if (b.getState() instanceof TileState) {
Optional<String> blockData = SlimefunPlugin.getBlockDataService().getBlockData((TileState) b.getState());
if (blockData.isPresent()) return blockData.get();
}
return checkID(b.getLocation());
}
public static boolean check(Block block, String slimefunItem) {
return check(block.getLocation(), slimefunItem);
String id = checkID(block);
return id != null && id.equals(slimefunItem);
}
public static String checkID(Location l) {