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

Reduced technical debt

This commit is contained in:
TheBusyBiscuit 2019-09-21 16:21:10 +02:00
parent 0f7f31f8ba
commit a31a30b6af
10 changed files with 80 additions and 101 deletions

View File

@ -21,35 +21,32 @@ public class MultiBlock {
this.blocks = build;
this.isSymmetric = isSymmetric(build);
//Hacky, the other constructor should be used
for (int i = 1; i < 9; i +=3)
{
if (trigger.equals(build[i]))
{
for (int i = 1; i < 9; i +=3) {
if (trigger == build[i]) {
switch (i) {
case 1:
this.trigger = BlockFace.DOWN;
break;
return;
case 4:
this.trigger = BlockFace.SELF;
break;
return;
case 7:
this.trigger = BlockFace.UP;
return;
default:
break;
}
break;
}
}
}
public MultiBlock(Material[] build, BlockFace trigger)
{
public MultiBlock(Material[] build, BlockFace trigger) {
this.blocks = build;
this.trigger = trigger;
this.isSymmetric = isSymmetric(build);
}
private static boolean isSymmetric(Material[] blocks)
{
private static boolean isSymmetric(Material[] blocks) {
return blocks[0] == blocks[2]
&& blocks[3] == blocks[5]
&& blocks[6] == blocks[8];
@ -106,8 +103,7 @@ public class MultiBlock {
return true;
}
public boolean isSymmetric()
{
public boolean isSymmetric() {
return this.isSymmetric;
}

View File

@ -35,7 +35,6 @@ import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
public class SlimefunItem {
private String id;
private String hash;
private ItemState state;
private ItemStack item;
private Category category;
@ -106,16 +105,6 @@ public class SlimefunItem {
this.hidden = hidden;
}
/**
* Returns the identifier of this SlimefunItem.
*
* @return the identifier of this SlimefunItem
*
* @since 4.0
* @deprecated As of 4.1.11, renamed to {@link #getID()} for better name convenience.
*/
@Deprecated
public String getName() { return id; }
/**
* Returns the identifier of this SlimefunItem.
*
@ -125,20 +114,12 @@ public class SlimefunItem {
*/
public String getID() { return id; }
@Deprecated
public String getHash() { return hash; }
public ItemState getState() { return state; }
public ItemStack getItem() { return item; }
public Category getCategory() { return category; }
public ItemStack[] getRecipe() { return recipe; }
public RecipeType getRecipeType() { return recipeType; }
/**
* @since 4.0
* @deprecated As of 4.1.11, renamed to {@link #getRecipeOutput()} for better name convenience.
*/
@Deprecated
public ItemStack getCustomOutput() { return recipeOutput; }
/**
* @since 4.1.11, rename of {@link #getCustomOutput()}.
*/
@ -160,12 +141,7 @@ public class SlimefunItem {
public List<String> getNoPermissionTooltip() { return noPermissionTooltip; }
public Set<ItemHandler> getHandlers() { return itemhandlers; }
public boolean isTicking() { return ticking; }
/**
* @since 4.0
* @deprecated As of 4.1.11, renamed to {@link #getBlockTicker()} for better name convenience.
*/
@Deprecated
public BlockTicker getTicker() { return blockTicker; }
/**
* @since 4.1.11, rename of {@link #getTicker()}.
*/
@ -265,11 +241,6 @@ public class SlimefunItem {
this.research = r;
}
@Deprecated
public void setHash(String hash) {
this.hash = hash;
}
public void setRecipe(ItemStack[] recipe) {
this.recipe = recipe;
}

View File

@ -13,7 +13,7 @@ public interface DamageableItem {
boolean isDamageable();
default void damageItem(Player p, ItemStack item) {
if (item != null && item.getType() != null && item.getType() != Material.AIR && item.getAmount() > 0 && isDamageable()) {
if (item != null && item.getType() != Material.AIR && item.getAmount() > 0 && isDamageable()) {
if (item.getEnchantments().containsKey(Enchantment.DURABILITY) && Math.random() * 100 <= (60 + Math.floorDiv(40, (item.getEnchantmentLevel(Enchantment.DURABILITY) + 1)))) {
return;
}

View File

@ -1,7 +1,5 @@
package me.mrCookieSlime.Slimefun.Objects.SlimefunItem.machines.electric;
import java.util.logging.Level;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
@ -21,7 +19,6 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.interfaces.InventoryBlock;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
import me.mrCookieSlime.Slimefun.Setup.SlimefunManager;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.api.energy.ChargableBlock;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenu;
import me.mrCookieSlime.Slimefun.api.inventory.BlockMenuPreset;
@ -72,11 +69,7 @@ public class AutoBreeder extends SlimefunItem implements InventoryBlock {
@Override
public void tick(Block b, SlimefunItem sf, Config data) {
try {
AutoBreeder.this.tick(b);
} catch (Exception x) {
Slimefun.getLogger().log(Level.SEVERE, "An Error occured while ticking an Auto Breeder for Slimefun " + Slimefun.getVersion(), x);
}
AutoBreeder.this.tick(b);
}
@Override
@ -86,7 +79,7 @@ public class AutoBreeder extends SlimefunItem implements InventoryBlock {
});
}
protected void tick(Block b) throws Exception {
protected void tick(Block b) {
for (Entity n : b.getWorld().getNearbyEntities(b.getLocation(), 4.0, 2.0, 4.0, n -> n instanceof Animals && n.isValid() && ((Animals) n).isAdult() && !((Animals) n).isLoveMode())) {
for (int slot : getInputSlots()) {
if (SlimefunManager.isItemSimiliar(BlockStorage.getInventory(b).getItemInSlot(slot), SlimefunItems.ORGANIC_FOOD, false)) {

View File

@ -11,6 +11,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.plugin.Plugin;
@ -36,25 +37,31 @@ public class CSCoreLibLoader {
}
}
public boolean load() {
if (plugin.getServer().getPluginManager().isPluginEnabled("CS-CoreLib")) return true;
public boolean load(Logger logger) {
if (plugin.getServer().getPluginManager().isPluginEnabled("CS-CoreLib")) {
return true;
}
else {
Slimefun.getLogger().log(Level.INFO, " ");
Slimefun.getLogger().log(Level.INFO, "#################### - INFO - ####################");
Slimefun.getLogger().log(Level.INFO, " ");
Slimefun.getLogger().log(Level.INFO, plugin.getName() + " could not be loaded.");
Slimefun.getLogger().log(Level.INFO, "It appears that you have not installed CS-CoreLib");
Slimefun.getLogger().log(Level.INFO, "Your Server will now try to download and install");
Slimefun.getLogger().log(Level.INFO, "CS-CoreLib for you.");
Slimefun.getLogger().log(Level.INFO, "You will be asked to restart your Server when it's finished.");
Slimefun.getLogger().log(Level.INFO, "If this somehow fails, please download and install CS-CoreLib manually:");
Slimefun.getLogger().log(Level.INFO, "https://dev.bukkit.org/projects/cs-corelib");
Slimefun.getLogger().log(Level.INFO, " ");
Slimefun.getLogger().log(Level.INFO, "#################### - INFO - ####################");
Slimefun.getLogger().log(Level.INFO, " ");
logger.log(Level.INFO, " ");
logger.log(Level.INFO, "#################### - INFO - ####################");
logger.log(Level.INFO, " ");
logger.log(Level.INFO, plugin.getName() + " could not be loaded (yet).");
logger.log(Level.INFO, "It appears that you have not installed CS-CoreLib.");
logger.log(Level.INFO, "Your Server will now try to download and install");
logger.log(Level.INFO, "CS-CoreLib for you.");
logger.log(Level.INFO, "You will be asked to restart your Server when it's finished.");
logger.log(Level.INFO, "If this somehow fails, please download and install CS-CoreLib manually:");
logger.log(Level.INFO, "https://dev.bukkit.org/projects/cs-corelib");
logger.log(Level.INFO, " ");
logger.log(Level.INFO, "#################### - INFO - ####################");
logger.log(Level.INFO, " ");
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, () -> {
if (connect()) install();
if (connect()) {
install();
}
}, 10L);
return false;
}
}

View File

@ -84,9 +84,9 @@ public final class SlimefunPlugin extends JavaPlugin {
private Config whitelist;
private Config config;
private final GPSNetwork gps = new GPSNetwork();
private GPSNetwork gps;
private ProtectionManager protections;
private Utilities utilities = new Utilities();
private Utilities utilities;
private Settings settings;
private SlimefunHooks hooks;
@ -95,8 +95,7 @@ public final class SlimefunPlugin extends JavaPlugin {
@Override
public void onEnable() {
CSCoreLibLoader loader = new CSCoreLibLoader(this);
if (loader.load()) {
if (new CSCoreLibLoader(this).load(getLogger())) {
String currentVersion = ReflectionUtils.getVersion();
@ -156,6 +155,10 @@ public final class SlimefunPlugin extends JavaPlugin {
// Setup messages.yml
local = new SlimefunLocalization(this);
// Setting up other stuff
utilities = new Utilities();
gps = new GPSNetwork();
// Setting up bStats
new Metrics(this);
@ -320,6 +323,13 @@ public final class SlimefunPlugin extends JavaPlugin {
// Do not show /sf elevator command in our Log, it could get quite spammy
CSCoreLib.getLib().filterLog("([A-Za-z0-9_]{3,16}) issued server command: /sf elevator (.{0,})");
}
else {
getCommand("slimefun").setExecutor((sender, cmd, label, args) -> {
sender.sendMessage("You have forgotten to install CS-CoreLib! Slimefun is disabled.");
sender.sendMessage("https://dev.bukkit.org/projects/cs-corelib");
return true;
});
}
}
@Override

View File

@ -58,8 +58,7 @@ public class BlockInfoConfig extends Config {
@Override
public Object getValue(String path) {
checkPath(path);
return data.get(path);
return getString(path);
}
@Override

View File

@ -55,10 +55,12 @@ public abstract class BlockMenuPreset extends ChestMenu {
}
public int[] getSlotsAccessedByItemTransport(BlockMenu menu, ItemTransportFlow flow, ItemStack item) {
// This method will default to this method, can be overridden though
return this.getSlotsAccessedByItemTransport(flow);
}
public int[] getSlotsAccessedByItemTransport(UniversalBlockMenu menu, ItemTransportFlow flow, ItemStack item) {
// This method will default to this method, can be overridden though
return this.getSlotsAccessedByItemTransport(flow);
}

View File

@ -46,7 +46,7 @@ public class BlockListener implements Listener {
@EventHandler
public void onPistonExtend(BlockPistonExtendEvent e) {
for (Block b : e.getBlocks()) {
for (Block b: e.getBlocks()) {
if (BlockStorage.hasBlockInfo(b) || b.getRelative(e.getDirection()).getType() == Material.AIR && BlockStorage.hasBlockInfo(b.getRelative(e.getDirection()))) {
e.setCancelled(true);
return;
@ -57,7 +57,7 @@ public class BlockListener implements Listener {
@EventHandler
public void onPistonRetract(BlockPistonRetractEvent e) {
if (e.isSticky()) {
for (Block b : e.getBlocks()) {
for (Block b: e.getBlocks()) {
if (BlockStorage.hasBlockInfo(b) || b.getRelative(e.getDirection()).getType() == Material.AIR && BlockStorage.hasBlockInfo(b.getRelative(e.getDirection()))) {
e.setCancelled(true);
return;
@ -68,31 +68,32 @@ public class BlockListener implements Listener {
@EventHandler
public void onRightClick(PlayerInteractEvent e) {
if (e.getHand() != EquipmentSlot.HAND) return;
if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
if (e.getHand() != EquipmentSlot.HAND) return;
Player p = e.getPlayer();
Block b = e.getClickedBlock();
LinkedList<MultiBlock> multiblocks = new LinkedList<>();
Player p = e.getPlayer();
Block b = e.getClickedBlock();
LinkedList<MultiBlock> multiblocks = new LinkedList<>();
for (MultiBlock mb: MultiBlock.list()) {
Block center = b.getRelative(mb.getTriggerBlock());
for (MultiBlock mb: MultiBlock.list()) {
Block center = b.getRelative(mb.getTriggerBlock());
if (compareMaterials(center, mb.getBuild(), mb.isSymmetric())) {
multiblocks.add(mb);
}
if (compareMaterials(center, mb.getBuild(), mb.isSymmetric())) {
multiblocks.add(mb);
}
}
if (!multiblocks.isEmpty()) {
e.setCancelled(true);
MultiBlock multiblock = multiblocks.getLast();
for (ItemHandler handler: SlimefunItem.getHandlers("MultiBlockInteractionHandler")) {
if (((MultiBlockInteractionHandler) handler).onInteract(p, multiblock, b)) break;
}
if (!multiblocks.isEmpty()) {
e.setCancelled(true);
MultiBlock multiblock = multiblocks.getLast();
for (ItemHandler handler: SlimefunItem.getHandlers("MultiBlockInteractionHandler")) {
if (((MultiBlockInteractionHandler) handler).onInteract(p, multiblock, b)) break;
}
MultiBlockInteractEvent event = new MultiBlockInteractEvent(p, multiblock, b);
Bukkit.getPluginManager().callEvent(event);
}
MultiBlockInteractEvent event = new MultiBlockInteractEvent(p, multiblock, b);
Bukkit.getPluginManager().callEvent(event);
}
}
}

View File

@ -142,9 +142,9 @@ public class TalismanListener implements Listener {
ItemStack item = e.getPlayer().getInventory().getItemInMainHand();
int fortune = 1;
if (item != null) {
if (item.getType() != Material.AIR && item.getAmount() > 0) {
if (item.getEnchantments().containsKey(Enchantment.LOOT_BONUS_BLOCKS) && !item.getEnchantments().containsKey(Enchantment.SILK_TOUCH)) {
fortune =random.nextInt(item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS) + 2) - 1;
fortune = random.nextInt(item.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS) + 2) - 1;
if (fortune <= 0) fortune = 1;
fortune = (e.getBlock().getType() == Material.LAPIS_ORE ? 4 + random.nextInt(5) : 1) * (fortune + 1);
}