Added ItemFrame Invisibility Option

This commit is contained in:
jameslfc19 2020-07-14 22:56:58 +01:00
parent a5df23d1d1
commit f3c04c0783
10 changed files with 65 additions and 5 deletions

View File

@ -1,6 +1,10 @@
package com.jamesdpeters.minecraft.chests;
import org.bukkit.entity.ItemFrame;
public interface NMSProvider {
ChestOpener getChestOpener();
MaterialChecker getMaterialChecker();
void setItemFrameVisible(ItemFrame itemFrame, boolean visible);
}

View File

@ -3,6 +3,7 @@ package com.jamesdpeters.minecraft.chests.v1_14_R1;
import com.jamesdpeters.minecraft.chests.ChestOpener;
import com.jamesdpeters.minecraft.chests.MaterialChecker;
import com.jamesdpeters.minecraft.chests.NMSProvider;
import org.bukkit.entity.ItemFrame;
public class NMSProviderImpl implements NMSProvider {
@Override
@ -14,4 +15,9 @@ public class NMSProviderImpl implements NMSProvider {
public MaterialChecker getMaterialChecker() {
return new MaterialChecker_1_14();
}
@Override
public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) {
//Not support in 1.14
}
}

View File

@ -3,6 +3,7 @@ package com.jamesdpeters.minecraft.chests.v1_15_R1;
import com.jamesdpeters.minecraft.chests.ChestOpener;
import com.jamesdpeters.minecraft.chests.MaterialChecker;
import com.jamesdpeters.minecraft.chests.NMSProvider;
import org.bukkit.entity.ItemFrame;
public class NMSProviderImpl implements NMSProvider {
@Override
@ -14,4 +15,9 @@ public class NMSProviderImpl implements NMSProvider {
public MaterialChecker getMaterialChecker() {
return new MaterialChecker_1_15();
}
@Override
public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) {
//Not supported in 1.15
}
}

View File

@ -3,6 +3,7 @@ package com.jamesdpeters.minecraft.chests.v1_16_R1;
import com.jamesdpeters.minecraft.chests.ChestOpener;
import com.jamesdpeters.minecraft.chests.MaterialChecker;
import com.jamesdpeters.minecraft.chests.NMSProvider;
import org.bukkit.entity.ItemFrame;
public class NMSProviderImpl implements NMSProvider {
@ -15,4 +16,9 @@ public class NMSProviderImpl implements NMSProvider {
public MaterialChecker getMaterialChecker() {
return new MaterialChecker_1_16();
}
@Override
public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) {
itemFrame.setVisible(visible);
}
}

View File

@ -88,7 +88,7 @@ public class ChestsPlusPlus extends JavaPlugin {
ApiSpecific.init();
//Remove entities that could have been left behind from bad save files/crashes etc.
Utils.removeEntities();
Utils.fixEntities();
//Register commands
new ChestLinkCommand().register(this);

View File

@ -11,9 +11,10 @@ public class ApiSpecific {
private static MaterialChecker materialChecker;
private static ChestOpener chestOpener;
private static NMSProvider nmsProvider;
public static void init(){
NMSProvider nmsProvider = API.getNMSProvider();
nmsProvider = API.getNMSProvider();
if(nmsProvider == null) nmsProvider = new NMSProviderDefault();
materialChecker = nmsProvider.getMaterialChecker();
chestOpener = nmsProvider.getChestOpener();
@ -25,5 +26,8 @@ public class ApiSpecific {
public static ChestOpener getChestOpener() {
return chestOpener;
}
public static NMSProvider getNmsProvider() {
return nmsProvider;
}
public static String getApiVersion(){ return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; }
}

View File

@ -4,6 +4,7 @@ import com.jamesdpeters.minecraft.chests.ChestOpener;
import com.jamesdpeters.minecraft.chests.MaterialChecker;
import com.jamesdpeters.minecraft.chests.NMSProvider;
import com.jamesdpeters.minecraft.chests.v1_16_R1.MaterialChecker_1_16;
import org.bukkit.entity.ItemFrame;
public class NMSProviderDefault implements NMSProvider {
@Override
@ -18,4 +19,9 @@ public class NMSProviderDefault implements NMSProvider {
//Return the current latest MaterialChecker when an newer server implementation is found.
return new MaterialChecker_1_16();
}
@Override
public void setItemFrameVisible(ItemFrame itemFrame, boolean visible) {
//Not supported in Bukkit api 1.14.
}
}

View File

@ -1,7 +1,9 @@
package com.jamesdpeters.minecraft.chests.listeners;
import com.jamesdpeters.minecraft.chests.ChestsPlusPlus;
import com.jamesdpeters.minecraft.chests.api.ApiSpecific;
import com.jamesdpeters.minecraft.chests.filters.HopperFilter;
import com.jamesdpeters.minecraft.chests.misc.Settings;
import com.jamesdpeters.minecraft.chests.serialize.Config;
import com.jamesdpeters.minecraft.chests.misc.Utils;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
@ -15,10 +17,14 @@ import org.bukkit.block.Block;
import org.bukkit.block.Hopper;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.ItemFrame;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityEvent;
import org.bukkit.event.entity.EntityInteractEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
@ -78,6 +84,9 @@ public class HopperListener implements Listener {
if(!(attachedBlock.getState() instanceof Hopper)) return;
Rotation rotation = itemFrame.getRotation();
//Set ItemFrame invisible based on config.
ApiSpecific.getNmsProvider().setItemFrameVisible(itemFrame, !Settings.isFilterItemFrameInvisible());
//ItemFrame event acts weird, it returns the values of the itemframe *before* the event. So we have to calculate what the next state will be.
if(!itemFrame.getItem().getType().equals(Material.AIR)) rotation = rotation.rotateClockwise();

View File

@ -13,6 +13,7 @@ public class Settings {
private static String RUN_HOPPERS_UNLOADED_CHUNKS = "run-hoppers-unloaded-chunks";
private static String SHOULD_CHEST_ARMOUR_STAND = "display_chestlink_armour_stands";
private static String SHOULD_AUTOCRAFT_ARMOUR_STAND = "display_chestlink_armour_stands";
private static String INVISIBLE_FILTER_ITEM_FRAMES = "set-filter-itemframe-invisible";
private static Settings cf;
private FileConfiguration configuration;
@ -26,6 +27,7 @@ public class Settings {
private static boolean runHoppersInUnloadedChunks;
private static boolean shouldDisplayChestLinkStand;
private static boolean shouldDisplayAutoCraftStand;
private static boolean filterItemFrameInvisible;
public static void initConfig(Plugin plugin){
cf = new Settings();
@ -37,10 +39,11 @@ public class Settings {
cf.configuration.addDefault(CHECK_UPDATE_PERIOD,60*60);
cf.configuration.addDefault(LIMIT_CHESTS,false);
cf.configuration.addDefault(LIMIT_CHESTS_NUMBER,0);
cf.configuration.addDefault(SHOULD_ANIMATE_ALL_CHESTS,false);
cf.configuration.addDefault(SHOULD_ANIMATE_ALL_CHESTS,true);
cf.configuration.addDefault(RUN_HOPPERS_UNLOADED_CHUNKS,false);
cf.configuration.addDefault(SHOULD_CHEST_ARMOUR_STAND,true);
cf.configuration.addDefault(SHOULD_AUTOCRAFT_ARMOUR_STAND,true);
cf.configuration.addDefault(INVISIBLE_FILTER_ITEM_FRAMES, false);
cf.configuration.options().copyDefaults(true);
cf.plugin.saveConfig();
@ -63,6 +66,7 @@ public class Settings {
runHoppersInUnloadedChunks = cf.configuration.getBoolean(RUN_HOPPERS_UNLOADED_CHUNKS);
shouldDisplayChestLinkStand = cf.configuration.getBoolean(SHOULD_CHEST_ARMOUR_STAND);
shouldDisplayAutoCraftStand = cf.configuration.getBoolean(SHOULD_AUTOCRAFT_ARMOUR_STAND);
filterItemFrameInvisible = cf.configuration.getBoolean(INVISIBLE_FILTER_ITEM_FRAMES);
}
/**
@ -86,4 +90,7 @@ public class Settings {
public static boolean isShouldDisplayAutoCraftStand() {
return shouldDisplayAutoCraftStand;
}
public static boolean isFilterItemFrameInvisible() {
return filterItemFrameInvisible;
}
}

View File

@ -8,6 +8,7 @@ import com.jamesdpeters.minecraft.chests.interfaces.VirtualInventoryHolder;
import com.jamesdpeters.minecraft.chests.storage.chestlink.ChestLinkStorage;
import org.bukkit.*;
import org.bukkit.block.*;
import org.bukkit.entity.ItemFrame;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
@ -170,9 +171,13 @@ public class Utils {
/**
* Removes all entities that contain a value of 1 under the Values.PluginKey key.
* Updates all Item Frames with invisibility status.
*/
public static void removeEntities(){
Bukkit.getServer().getWorlds().forEach(Utils::removeEntities);
public static void fixEntities(){
Bukkit.getServer().getWorlds().forEach(world -> {
removeEntities(world);
setItemFrames(world);
});
}
public static void removeEntities(World world){
@ -182,6 +187,13 @@ public class Utils {
});
}
public static void setItemFrames(World world){
world.getEntities().stream().filter(entity ->
(entity instanceof ItemFrame
&& entity.getLocation().getBlock().getRelative(((ItemFrame) entity).getAttachedFace()).getState() instanceof Hopper))
.forEach(entity -> ApiSpecific.getNmsProvider().setItemFrameVisible((ItemFrame) entity, !Settings.isFilterItemFrameInvisible()));
}
public static List<String> filterList(List<String> list, String phrase){
return list.stream().filter(s -> s.contains(phrase)).collect(Collectors.toList());
}