Rewrite of the Custom item into RecipeItem

and Ingredient with subclasses
Implemented adding custom items to Ingredients
Added support for plugin-items
This commit is contained in:
Sn0wStorm
2019-11-06 19:44:52 +01:00
parent d0263c611c
commit 7f2f9d75dd
41 changed files with 2010 additions and 624 deletions
@@ -0,0 +1,43 @@
package com.dre.brewery.integration.barrel;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import com.dre.brewery.P;
import vg.civcraft.mc.citadel.Citadel;
import vg.civcraft.mc.citadel.ReinforcementManager;
import vg.civcraft.mc.citadel.reinforcement.NullReinforcement;
import vg.civcraft.mc.citadel.reinforcement.PlayerReinforcement;
import vg.civcraft.mc.citadel.reinforcement.Reinforcement;
/**
* Basic Citadel support to prevent randos from stealing your barrel aging brews
*
* @author ProgrammerDan
*/
public class CitadelBarrel {
static P brewery = P.p;
public static boolean checkAccess(Player player, Block sign) {
ReinforcementManager manager = Citadel.getReinforcementManager();
Reinforcement rein = manager.getReinforcement(sign);
if (rein == null) return true; // no protections in place.
if (rein instanceof PlayerReinforcement) {
PlayerReinforcement prein = (PlayerReinforcement) rein;
if (prein.canAccessChests(player)) {
return true;
}
} else if (rein instanceof NullReinforcement) {
return true;
}
// no support for multiblock atm, would require namelayer support.
// special locked, or no access.
brewery.msg(player, brewery.languageReader.get("Error_NoBarrelAccess"));
return false;
}
}
@@ -0,0 +1,46 @@
package com.dre.brewery.integration.barrel;
import com.dre.brewery.P;
import com.dre.brewery.api.events.barrel.BarrelAccessEvent;
import me.ryanhamshire.GriefPrevention.Claim;
import me.ryanhamshire.GriefPrevention.GriefPrevention;
import me.ryanhamshire.GriefPrevention.PlayerData;
import org.bukkit.entity.Player;
public class GriefPreventionBarrel {
private static P brewery = P.p;
public static boolean checkAccess(BarrelAccessEvent event) {
GriefPrevention griefPrevention = GriefPrevention.instance;
Player player = event.getPlayer();
PlayerData playerData = griefPrevention.dataStore.getPlayerData(player.getUniqueId());
if (!griefPrevention.claimsEnabledForWorld(player.getWorld()) || playerData.ignoreClaims || !griefPrevention.config_claims_preventTheft) {
return true;
}
// block container use during pvp combat
if (playerData.inPvpCombat()) {
return false;
}
// check permissions for the claim the Barrel is in
Claim claim = griefPrevention.dataStore.getClaimAt(event.getSpigot().getLocation(), false, playerData.lastClaim);
if (claim != null) {
playerData.lastClaim = claim;
String noContainersReason = claim.allowContainers(player);
if (noContainersReason != null) {
return false;
}
}
// drop any pvp protection, as the player opens a barrel
if (playerData.pvpImmune) {
playerData.pvpImmune = false;
}
return true;
}
}
@@ -0,0 +1,102 @@
package com.dre.brewery.integration.barrel;
import com.dre.brewery.Barrel;
import com.dre.brewery.P;
import com.griefcraft.listeners.LWCPlayerListener;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.Flag;
import com.griefcraft.model.Protection;
import com.griefcraft.scripting.event.LWCProtectionDestroyEvent;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventException;
import org.bukkit.event.HandlerList;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredListener;
public class LWCBarrel {
public static boolean denyDestroy(Player player, Barrel barrel) {
LWC lwc = LWC.getInstance();
Block sign = barrel.getBody().getSignOfSpigot();
//if (!Boolean.parseBoolean(lwc.resolveProtectionConfiguration(sign, "ignoreBlockDestruction"))) {
Protection protection = lwc.findProtection(sign);
if (protection != null) {
boolean canAccess = lwc.canAccessProtection(player, protection);
boolean canAdmin = lwc.canAdminProtection(player, protection);
try {
LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection, LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION, canAccess, canAdmin);
lwc.getModuleLoader().dispatchEvent(evt);
if (evt.isCancelled()) {
return true;
}
} catch (Exception e) {
lwc.sendLocale(player, "protection.internalerror", "id", "BLOCK_BREAK");
P.p.errorLog("Failed to dispatch LWCProtectionDestroyEvent");
e.printStackTrace();
return true;
}
}
//}
return false;
}
public static boolean checkAccess(Player player, Block sign, Plugin plugin) {
LWC lwc = LWC.getInstance();
// Disallow Chest Access with these permissions
if (!lwc.hasPermission(player, "lwc.protect") && lwc.hasPermission(player, "lwc.deny") && !lwc.isAdmin(player) && !lwc.isMod(player)) {
lwc.sendLocale(player, "protection.interact.error.blocked");
return false;
}
// We just fake a BlockInteractEvent on the Sign for LWC, it handles it nicely. Otherwise we could copy LWCs listener in here...
PlayerInteractEvent lwcEvent = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, new ItemStack(Material.AIR), sign, BlockFace.EAST);
for (RegisteredListener listener : HandlerList.getRegisteredListeners(plugin)) {
if (listener.getListener() instanceof LWCPlayerListener) {
try {
listener.callEvent(lwcEvent);
if (lwcEvent.isCancelled()) {
return false;
}
} catch (EventException e) {
lwc.sendLocale(player, "protection.internalerror", "id", "PLAYER_INTERACT");
P.p.errorLog("Block Interact could not be passed to LWC");
e.printStackTrace();
return false;
}
}
}
return true;
}
// If a Barrel is destroyed without player
public static void remove(Barrel barrel) {
Protection protection = LWC.getInstance().findProtection(barrel.getBody().getSignOfSpigot());
if (protection != null) {
protection.remove();
}
}
// Returns true if the block that exploded should not be removed
public static boolean denyExplosion(Barrel barrel) {
Protection protection = LWC.getInstance().findProtection(barrel.getBody().getSignOfSpigot());
return protection != null && !protection.hasFlag(Flag.Type.ALLOWEXPLOSIONS);
}
// Returns true if the block that was destroyed should not be removed
public static boolean denyDestroyOther(Barrel barrel) {
return LWC.getInstance().findProtection(barrel.getBody().getSignOfSpigot()) != null;
}
}
@@ -0,0 +1,128 @@
package com.dre.brewery.integration.barrel;
import com.dre.brewery.utility.LegacyUtil;
import com.dre.brewery.P;
import de.diddiz.LogBlock.Actor;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import de.diddiz.LogBlock.Consumer;
import de.diddiz.LogBlock.LogBlock;
import de.diddiz.LogBlock.Logging;
import static de.diddiz.LogBlock.config.Config.isLogging;
import de.diddiz.util.BukkitUtils;
import static de.diddiz.util.BukkitUtils.compareInventories;
import static de.diddiz.util.BukkitUtils.compressInventory;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@SuppressWarnings("JavaReflectionMemberAccess")
public class LogBlockBarrel {
private static final List<LogBlockBarrel> opened = new ArrayList<>();
public static Consumer consumer = LogBlock.getInstance().getConsumer();
private static Method rawData;
private static Method queueChestAccess;
static {
if (!P.use1_13) {
try {
rawData = BukkitUtils.class.getDeclaredMethod("rawData", ItemStack.class);
queueChestAccess = Consumer.class.getDeclaredMethod("queueChestAccess", String.class, Location.class, int.class, short.class, short.class, short.class);
} catch (NoSuchMethodException e) {
P.p.errorLog("Failed to hook into LogBlock to log barrels. Logging barrel contents is not going to work.");
P.p.errorLog("Brewery was tested with version 1.12 to 1.13.1 of LogBlock.");
P.p.errorLog("Disable LogBlock support in the configuration file and type /brew reload.");
e.printStackTrace();
}
}
}
private HumanEntity player;
private ItemStack[] items;
private Location loc;
public LogBlockBarrel(HumanEntity player, ItemStack[] items, Location spigotLoc) {
this.player = player;
this.items = items;
this.loc = spigotLoc;
opened.add(this);
}
private void compareInv(final ItemStack[] after) {
if (consumer == null) {
return;
}
final ItemStack[] diff = compareInventories(items, after);
for (final ItemStack item : diff) {
if (!P.use1_13) {
try {
//noinspection deprecation
queueChestAccess.invoke(consumer, player.getName(), loc, LegacyUtil.getBlockTypeIdAt(loc), (short) item.getType().getId(), (short) item.getAmount(), rawData.invoke(null, item));
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
} else {
ItemStack i2 = item;
if (item.getAmount() < 0) {
i2 = item.clone();
i2.setAmount(Math.abs(item.getAmount()));
}
consumer.queueChestAccess(Actor.actorFromEntity(player), loc, loc.getBlock().getBlockData(), i2, item.getAmount() < 0);
}
}
}
public static LogBlockBarrel get(HumanEntity player) {
for (LogBlockBarrel open : opened) {
if (open.player.equals(player)) {
return open;
}
}
return null;
}
public static void openBarrel(HumanEntity player, Inventory inv, Location spigotLoc) {
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return;
new LogBlockBarrel(player, compressInventory(inv.getContents()), spigotLoc);
}
public static void closeBarrel(HumanEntity player, Inventory inv) {
if (!isLogging(player.getWorld(), Logging.CHESTACCESS)) return;
LogBlockBarrel open = get(player);
if (open != null) {
open.compareInv(compressInventory(inv.getContents()));
opened.remove(open);
}
}
public static void breakBarrel(Player player, ItemStack[] contents, Location spigotLoc) {
if (consumer == null) {
return;
}
if (!isLogging(spigotLoc.getWorld(), Logging.CHESTACCESS)) return;
final ItemStack[] items = compressInventory(contents);
for (final ItemStack item : items) {
if (!P.use1_13) {
try {
//noinspection deprecation
queueChestAccess.invoke(consumer, player.getName(), spigotLoc, LegacyUtil.getBlockTypeIdAt(spigotLoc), (short) item.getType().getId(), (short) (item.getAmount() * -1), rawData.invoke(null, item));
} catch(IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
} else {
consumer.queueChestAccess(Actor.actorFromEntity(player), spigotLoc, spigotLoc.getBlock().getBlockData(), item, false);
}
}
}
public static void clear() {
opened.clear();
}
}
@@ -0,0 +1,9 @@
package com.dre.brewery.integration.barrel;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public interface WGBarrel {
boolean checkAccess(Player player, Block spigot, Plugin plugin);
}
@@ -0,0 +1,63 @@
package com.dre.brewery.integration.barrel;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.bukkit.block.Block;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.dre.brewery.P;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager;
public class WGBarrel5 implements WGBarrel {
private Method allows;
private Method canBuild;
private Method getApplicableRegions;
public WGBarrel5() {
try {
allows = ApplicableRegionSet.class.getMethod("allows", StateFlag.class, LocalPlayer.class);
canBuild = ApplicableRegionSet.class.getMethod("canBuild", LocalPlayer.class);
getApplicableRegions = RegionManager.class.getMethod("getApplicableRegions", Location.class);
} catch (NoSuchMethodException e) {
P.p.errorLog("Failed to Hook WorldGuard for Barrel Open Permissions! Opening Barrels will NOT work!");
P.p.errorLog("Brewery was tested with version 5.8, 6.1 to 7.0 of WorldGuard!");
P.p.errorLog("Disable the WorldGuard support in the config and do /brew reload");
e.printStackTrace();
}
}
@SuppressWarnings("deprecation")
public boolean checkAccess(Player player, Block spigot, Plugin plugin) {
WorldGuardPlugin wg = (WorldGuardPlugin) plugin;
if (!wg.getGlobalRegionManager().hasBypass(player, player.getWorld())) {
try {
Object region = getApplicableRegions.invoke(wg.getRegionManager(player.getWorld()), spigot.getLocation());
if (region != null) {
LocalPlayer localPlayer = wg.wrapPlayer(player);
if (!(Boolean) allows.invoke(region, DefaultFlag.CHEST_ACCESS, localPlayer)) {
if (!(Boolean) canBuild.invoke(region, localPlayer)) {
return false;
}
}
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return false;
}
}
return true;
}
}
@@ -0,0 +1,26 @@
package com.dre.brewery.integration.barrel;
import com.sk89q.worldguard.bukkit.RegionQuery;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.bukkit.permission.RegionPermissionModel;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
public class WGBarrel6 implements WGBarrel {
public boolean checkAccess(Player player, Block spigot, Plugin plugin) {
WorldGuardPlugin wg = (WorldGuardPlugin) plugin;
if (!wg.getGlobalStateManager().get(spigot.getWorld()).useRegions) return true; // Region support disabled
if (new RegionPermissionModel(wg, player).mayIgnoreRegionProtection(spigot.getWorld())) return true; // Whitelisted cause
RegionQuery query = wg.getRegionContainer().createQuery();
return query.testBuild(spigot.getLocation(), player, DefaultFlag.USE, DefaultFlag.CHEST_ACCESS);
}
}
@@ -0,0 +1,61 @@
package com.dre.brewery.integration.barrel;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import com.dre.brewery.P;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.internal.permission.RegionPermissionModel;
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
import com.sk89q.worldguard.protection.flags.Flags;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class WGBarrel7 implements WGBarrel {
private Method getWorldByName;
public WGBarrel7() {
try {
getWorldByName = WorldGuardPlatform.class.getDeclaredMethod("getWorldByName", String.class);
} catch (NoSuchMethodException e) {
getWorldByName = null;
}
}
public boolean checkAccess(Player player, Block spigot, Plugin plugin) {
WorldGuardPlugin wg = (WorldGuardPlugin) plugin;
WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform();
World world;
if (getWorldByName == null) {
world = platform.getMatcher().getWorldByName(spigot.getWorld().getName());
} else {
// Workaround for an older Beta Version of WorldGuard 7.0
try {
world = ((World) getWorldByName.invoke(platform, spigot.getWorld().getName()));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
P.p.msg(player, "Error in WorldGuard");
return false;
}
}
if (!platform.getGlobalStateManager().get(world).useRegions) return true; // Region support disabled
WorldEditPlugin we = JavaPlugin.getPlugin(WorldEditPlugin.class);
if (new RegionPermissionModel(we.wrapPlayer(player)).mayIgnoreRegionProtection(world)) return true; // Whitelisted cause
RegionQuery query = platform.getRegionContainer().createQuery();
return query.testBuild(new Location(world, spigot.getX(), spigot.getY(), spigot.getZ()), wg.wrapPlayer(player), Flags.USE, Flags.CHEST_ACCESS);
}
}