Refractor Utility

This commit is contained in:
Sn0wStorm
2019-10-16 20:07:09 +02:00
parent 7d177a3d9f
commit 437f521a9b
28 changed files with 147 additions and 128 deletions
+285
View File
@@ -0,0 +1,285 @@
package com.dre.brewery.utility;
import com.dre.brewery.BCauldron;
import com.dre.brewery.Barrel;
import com.dre.brewery.P;
import com.dre.brewery.api.events.barrel.BarrelDestroyEvent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.UUID;
public class BUtil {
/* **************************************** */
/* ********* ********* */
/* ********* Bukkit Utils ********* */
/* ********* ********* */
/* **************************************** */
/**
* Check if the Chunk of a Block is loaded !without loading it in the process!
*/
public static boolean isChunkLoaded(Block block) {
return block.getWorld().isChunkLoaded(block.getX() >> 4, block.getZ() >> 4);
}
public static String color(String msg) {
if (msg != null) {
msg = ChatColor.translateAlternateColorCodes('&', msg);
}
return msg;
}
/**
* Returns either uuid or Name of player, depending on bukkit version
*/
public static String playerString(Player player) {
if (P.useUUID) {
return player.getUniqueId().toString();
} else {
return player.getName();
}
}
// returns the Player if online
public static Player getPlayerfromString(String name) {
if (P.useUUID) {
try {
return Bukkit.getPlayer(UUID.fromString(name));
} catch (Exception e) {
return Bukkit.getPlayerExact(name);
}
}
return Bukkit.getPlayerExact(name);
}
/**
* Apply a Potion Effect, if player already has this effect, overwrite the existing effect.
*
* @param onlyIfStronger Optionally only overwrite if the new one is stronger, i.e. has higher level or longer duration
*/
public static void reapplyPotionEffect(Player player, PotionEffect effect, boolean onlyIfStronger) {
final PotionEffectType type = effect.getType();
if (player.hasPotionEffect(type)) {
PotionEffect plEffect;
if (P.use1_11) {
plEffect = player.getPotionEffect(type);
} else {
plEffect = player.getActivePotionEffects().stream().filter(e -> e.getType().equals(type)).findAny().get();
}
if (!onlyIfStronger ||
plEffect.getAmplifier() < effect.getAmplifier() ||
(plEffect.getAmplifier() == effect.getAmplifier() && plEffect.getDuration() < effect.getDuration())) {
player.removePotionEffect(type);
} else {
return;
}
}
effect.apply(player);
}
/* **************************************** */
/* ********* ********* */
/* ********* String Utils ********* */
/* ********* ********* */
/* **************************************** */
/**
* Returns the Index of a String from the list that contains this substring
*
* @param list The List in which to search for a substring
* @param substring Part of the String to search for in each of <tt>list</tt>
*/
public static int indexOfSubstring(List<String> list, String substring) {
if (list.isEmpty()) return -1;
for (int index = 0, size = list.size(); index < size; index++) {
String string = list.get(index);
if (string.contains(substring)) {
return index;
}
}
return -1;
}
/**
* Returns the index of a String from the list that starts with 'lineStart', returns -1 if not found;
*/
public static int indexOfStart(List<String> list, String lineStart) {
for (int i = 0, size = list.size(); i < size; i++) {
if (list.get(i).startsWith(lineStart)) {
return i;
}
}
return -1;
}
/* **************************************** */
/* ********* ********* */
/* ********* Brewery Utils ********* */
/* ********* ********* */
/* **************************************** */
/**
* create empty World save Sections
*/
public static void createWorldSections(ConfigurationSection section) {
for (World world : P.p.getServer().getWorlds()) {
String worldName = world.getName();
if (worldName.startsWith("DXL_")) {
worldName = getDxlName(worldName);
} else {
worldName = world.getUID().toString();
}
section.createSection(worldName);
}
}
/**
* Returns true if the Block can be destroyed by the Player or something else (null)
*
* @param player The Player that destroyed a Block, Null if no Player involved
* @return True if the Block can be destroyed
*/
public static boolean blockDestroy(Block block, Player player, BarrelDestroyEvent.Reason reason) {
Material type = block.getType();
if (type == Material.CAULDRON) {
// will only remove when existing
BCauldron.remove(block);
return true;
} else if (LegacyUtil.isFence(type)) {
// remove barrel and throw potions on the ground
Barrel barrel = Barrel.getBySpigot(block);
if (barrel != null) {
if (barrel.hasPermsDestroy(player, block, reason)) {
barrel.remove(null, player);
return true;
} else {
return false;
}
}
return true;
} else if (LegacyUtil.isSign(type)) {
// remove small Barrels
Barrel barrel2 = Barrel.getBySpigot(block);
if (barrel2 != null) {
if (!barrel2.isLarge()) {
if (barrel2.hasPermsDestroy(player, block, reason)) {
barrel2.remove(null, player);
return true;
} else {
return false;
}
} else {
barrel2.getBody().destroySign();
}
}
return true;
} else if (LegacyUtil.isWoodPlanks(type) || LegacyUtil.isWoodStairs(type)){
Barrel barrel3 = Barrel.getByWood(block);
if (barrel3 != null) {
if (barrel3.hasPermsDestroy(player, block, reason)) {
barrel3.remove(block, player);
} else {
return false;
}
}
}
return true;
}
/* **************************************** */
/* ********* ********* */
/* ********* Other Utils ********* */
/* ********* ********* */
/* **************************************** */
/**
* prints a list of Strings at the specified page
*
* @param sender The CommandSender to send the Page to
*/
public static void list(CommandSender sender, ArrayList<String> strings, int page) {
int pages = (int) Math.ceil(strings.size() / 7F);
if (page > pages || page < 1) {
page = 1;
}
sender.sendMessage(color("&7-------------- &f" + P.p.languageReader.get("Etc_Page") + " &6" + page + "&f/&6" + pages + " &7--------------"));
ListIterator<String> iter = strings.listIterator((page - 1) * 7);
for (int i = 0; i < 7; i++) {
if (iter.hasNext()) {
sender.sendMessage(color(iter.next()));
} else {
break;
}
}
}
/**
* gets the Name of a DXL World
*/
public static String getDxlName(String worldName) {
File dungeonFolder = new File(worldName);
if (dungeonFolder.isDirectory()) {
for (File file : dungeonFolder.listFiles()) {
if (!file.isDirectory()) {
if (file.getName().startsWith(".id_")) {
return file.getName().substring(1).toLowerCase();
}
}
}
}
return worldName;
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void saveFile(InputStream in, File dest, String name, boolean overwrite) throws IOException {
if (in == null) return;
if (!dest.exists()) {
dest.mkdirs();
}
File result = new File(dest, name);
if (result.exists()) {
if (overwrite) {
result.delete();
} else {
return;
}
}
OutputStream out = new FileOutputStream(result);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}
+251
View File
@@ -0,0 +1,251 @@
package com.dre.brewery.utility;
import com.dre.brewery.P;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.TreeSpecies;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Levelled;
import org.bukkit.material.Cauldron;
import org.bukkit.material.MaterialData;
import org.bukkit.material.Tree;
import org.bukkit.material.Wood;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import static com.dre.brewery.BCauldron.EMPTY;
import static com.dre.brewery.BCauldron.SOME;
import static com.dre.brewery.BCauldron.FULL;
@SuppressWarnings("JavaReflectionMemberAccess")
public class LegacyUtil {
private static Method GET_MATERIAL;
private static Method GET_BLOCK_TYPE_ID_AT;
private static Method SET_DATA;
static {
// <= 1.12.2 methods
// These will be rarely used
try {
GET_MATERIAL = Material.class.getDeclaredMethod("getMaterial", int.class);
GET_BLOCK_TYPE_ID_AT = World.class.getDeclaredMethod("getBlockTypeIdAt", Location.class);
} catch (NoSuchMethodException | SecurityException ignored) {
}
try {
SET_DATA = Class.forName(Bukkit.getServer().getClass().getPackage().getName() + ".block.CraftBlock").getDeclaredMethod("setData", byte.class);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException ignored) {
}
List<Material> planks = new ArrayList<>(6);
for (Material m : Material.values()) {
if (m.name().endsWith("PLANKS")) {
planks.add(m);
}
}
PLANKS = planks;
List<Material> fences = new ArrayList<>(7);
for (Material m : Material.values()) {
if (m.name().endsWith("FENCE")) {
fences.add(m);
}
}
FENCES = fences;
}
public static final Material MAGMA_BLOCK = get("MAGMA_BLOCK", "MAGMA");
public static final Material CAMPFIRE = get("CAMPFIRE");
public static final Material CLOCK = get("CLOCK", "WATCH");
public static final Material OAK_STAIRS = get("OAK_STAIRS", "WOOD_STAIRS");
public static final Material SPRUCE_STAIRS = get("SPRUCE_STAIRS", "SPRUCE_WOOD_STAIRS");
public static final Material BIRCH_STAIRS = get("BIRCH_STAIRS", "BIRCH_WOOD_STAIRS");
public static final Material JUNGLE_STAIRS = get("JUNGLE_STAIRS", "JUNGLE_WOOD_STAIRS");
public static final Material ACACIA_STAIRS = get("ACACIA_STAIRS");
public static final Material DARK_OAK_STAIRS = get("DARK_OAK_STAIRS");
public static final List<Material> PLANKS;
public static final List<Material> FENCES;
// Materials removed in 1.13
public static final Material STATIONARY_LAVA = get("STATIONARY_LAVA");
public static final Material SIGN_POST = get("SIGN_POST");
public static final Material WOOD = get("WOOD");
private static Material get(String name) {
try {
return Material.valueOf(name);
} catch (IllegalArgumentException e) {
return null;
}
}
private static Material get(String newName, String oldName) {
try {
return Material.valueOf(P.use1_13 ? newName : oldName);
} catch (IllegalArgumentException e) {
return null;
}
}
public static boolean isWoodPlanks(Material type) {
return (WOOD != null && type == WOOD) || PLANKS.contains(type);
}
public static boolean isWoodStairs(Material type) {
return type == OAK_STAIRS || type == SPRUCE_STAIRS || type == BIRCH_STAIRS || type == JUNGLE_STAIRS
|| (type == ACACIA_STAIRS && ACACIA_STAIRS != null) || (type == DARK_OAK_STAIRS && DARK_OAK_STAIRS != null);
}
public static boolean isFence(Material type) {
return FENCES.contains(type);
}
public static boolean isSign(Material type) {
return type.name().endsWith("SIGN") || (!P.use1_13 && type == SIGN_POST);
}
public static boolean isFireForCauldron(Block block) {
Material type = block.getType();
return type != null && (type == Material.FIRE || type == MAGMA_BLOCK || litCampfire(block) || isLava(type));
}
// LAVA and STATIONARY_LAVA are merged as of 1.13
public static boolean isLava(Material type) {
return type == Material.LAVA || (!P.use1_13 && type == STATIONARY_LAVA);
}
public static boolean litCampfire(Block block) {
if (block.getType() == CAMPFIRE) {
BlockData data = block.getBlockData();
if (data instanceof org.bukkit.block.data.Lightable) {
return ((org.bukkit.block.data.Lightable) data).isLit();
}
}
return false;
}
public static boolean areStairsInverted(Block block) {
if (!P.use1_13) {
@SuppressWarnings("deprecation")
MaterialData data = block.getState().getData();
return data instanceof org.bukkit.material.Stairs && (((org.bukkit.material.Stairs) data).isInverted());
} else {
BlockData data = block.getBlockData();
return data instanceof org.bukkit.block.data.type.Stairs && ((org.bukkit.block.data.type.Stairs) data).getHalf() == org.bukkit.block.data.type.Stairs.Half.TOP;
}
}
public static byte getWoodType(Block wood) throws NoSuchFieldError, NoClassDefFoundError {
TreeSpecies woodType;
if (P.use1_13 || isWoodStairs(wood.getType())) {
String material = wood.getType().name();
if (material.startsWith("OAK")) {
woodType = TreeSpecies.GENERIC;
} else if (material.startsWith("SPRUCE")) {
woodType = TreeSpecies.REDWOOD;
} else if (material.startsWith("BIRCH")) {
woodType = TreeSpecies.BIRCH;
} else if (material.startsWith("JUNGLE")) {
woodType = TreeSpecies.JUNGLE;
} else if (material.startsWith("ACACIA")) {
woodType = TreeSpecies.ACACIA;
} else if (material.startsWith("DARK_OAK")) {
woodType = TreeSpecies.DARK_OAK;
} else {
return 0;
}
} else {
@SuppressWarnings("deprecation")
MaterialData data = wood.getState().getData();
if (data instanceof Tree) {
woodType = ((Tree) data).getSpecies();
} else if (data instanceof Wood) {
woodType = ((Wood) data).getSpecies();
} else {
return 0;
}
}
switch (woodType) {
case GENERIC:
return 2;
case REDWOOD:
return 4;
case BIRCH:
return 1;
case JUNGLE:
return 3;
case ACACIA:
return 5;
case DARK_OAK:
return 6;
default:
return 0;
}
}
// 0 = empty, 1 = something in, 2 = full
public static byte getFillLevel(Block block) {
if (block.getType() != Material.CAULDRON) {
return EMPTY;
}
if (P.use1_13) {
Levelled cauldron = ((Levelled) block.getBlockData());
if (cauldron.getLevel() == 0) {
return EMPTY;
} else if (cauldron.getLevel() == cauldron.getMaximumLevel()) {
return FULL;
} else {
return SOME;
}
} else {
Cauldron cauldron = (Cauldron) block.getState().getData();
if (cauldron.isEmpty()) {
return EMPTY;
} else if (cauldron.isFull()) {
return FULL;
} else {
return SOME;
}
}
}
/*
* only used to convert a very old Datafile or config from a very old version
*/
public static Material getMaterial(int id) {
try {
return GET_MATERIAL != null ? (Material) GET_MATERIAL.invoke(null, id) : null;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return null;
}
}
// Only used for very old versions of LogBlock
public static int getBlockTypeIdAt(Location location) {
try {
return GET_BLOCK_TYPE_ID_AT != null ? (int) GET_BLOCK_TYPE_ID_AT.invoke(location.getWorld(), location) : 0;
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
return 0;
}
}
// Setting byte data to blocks for older versions
public static void setData(Block block, byte data) {
try {
SET_DATA.invoke(block, data);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ignored) {
}
}
}
@@ -0,0 +1,104 @@
package com.dre.brewery.utility;
import com.dre.brewery.P;
import org.bukkit.Color;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionType;
public class PotionColor {
public static final PotionColor PINK = new PotionColor(1, PotionType.REGEN, Color.FUCHSIA);
public static final PotionColor CYAN = new PotionColor(2, PotionType.SPEED, Color.AQUA);
public static final PotionColor ORANGE = new PotionColor(3, PotionType.FIRE_RESISTANCE, Color.ORANGE);
public static final PotionColor GREEN = new PotionColor(4, PotionType.POISON, Color.GREEN);
public static final PotionColor BRIGHT_RED = new PotionColor(5, PotionType.INSTANT_HEAL, Color.fromRGB(255,0,0));
public static final PotionColor BLUE = new PotionColor(6, PotionType.NIGHT_VISION, Color.NAVY);
public static final PotionColor BLACK = new PotionColor(8, PotionType.WEAKNESS, Color.BLACK);
public static final PotionColor RED = new PotionColor(9, PotionType.STRENGTH, Color.fromRGB(196,0,0));
public static final PotionColor GREY = new PotionColor(10, PotionType.SLOWNESS, Color.GRAY);
public static final PotionColor WATER = new PotionColor(11, P.use1_9 ? PotionType.WATER_BREATHING : null, Color.BLUE);
public static final PotionColor DARK_RED = new PotionColor(12, PotionType.INSTANT_DAMAGE, Color.fromRGB(128,0,0));
public static final PotionColor BRIGHT_GREY = new PotionColor(14, PotionType.INVISIBILITY, Color.SILVER);
private final int colorId;
private final PotionType type;
private final Color color;
PotionColor(int colorId, PotionType type, Color color) {
this.colorId = colorId;
this.type = type;
this.color = color;
}
public PotionColor(Color color) {
colorId = -1;
type = WATER.getType();
this.color = color;
}
// gets the Damage Value, that sets a color on the potion
// offset +32 is not accepted by brewer, so not further destillable
// Only for minecraft pre 1.9
public short getColorId(boolean destillable) {
if (destillable) {
return (short) (colorId + 64);
}
return (short) (colorId + 32);
}
public PotionType getType() {
return type;
}
public Color getColor() {
return color;
}
@SuppressWarnings("deprecation")
public void colorBrew(PotionMeta meta, ItemStack potion, boolean destillable) {
if (P.use1_9) {
meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS);
if (P.use1_11) {
// BasePotionData was only used for the Color, so starting with 1.12 we can use setColor instead
meta.setColor(getColor());
} else {
meta.setBasePotionData(new PotionData(getType()));
}
} else {
potion.setDurability(getColorId(destillable));
}
}
public static PotionColor fromString(String string) {
switch (string) {
case "PINK": return PINK;
case "CYAN": return CYAN;
case "ORANGE": return ORANGE;
case "GREEN": return GREEN;
case "BRIGHT_RED": return BRIGHT_RED;
case "BLUE": return BLUE;
case "BLACK": return BLACK;
case "RED": return RED;
case "GREY": return GREY;
case "WATER": return WATER;
case "DARK_RED": return DARK_RED;
case "BRIGHT_GREY": return BRIGHT_GREY;
default:
try{
if (string.length() >= 7) {
string = string.substring(1);
}
return new PotionColor(Color.fromRGB(
Integer.parseInt(string.substring( 0, 2 ), 16 ),
Integer.parseInt(string.substring( 2, 4 ), 16 ),
Integer.parseInt(string.substring( 4, 6 ), 16 )
));
} catch (Exception e) {
return WATER;
}
}
}
}