Skip to content

Instantly share code, notes, and snippets.

@Scarsz
Created July 28, 2020 22:18
Show Gist options
  • Save Scarsz/207c814a0d8a53f8f7690aefffe4c922 to your computer and use it in GitHub Desktop.
Save Scarsz/207c814a0d8a53f8f7690aefffe4c922 to your computer and use it in GitHub Desktop.
WorldGuard item-frame-interact flag plugin
package github.scarsz.handsofftheitemframes;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.flags.Flag;
import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.ItemFrame;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.plugin.java.JavaPlugin;
public final class Plugin extends JavaPlugin implements Listener {
private static StateFlag FLAG_ITEM_FRAME_INTERACT;
private RegionQuery regionQuery;
@Override
public void onLoad() {
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
try {
StateFlag flag = new StateFlag("item-frame-interact", true);
registry.register(flag);
FLAG_ITEM_FRAME_INTERACT = flag;
} catch (FlagConflictException e) {
Flag<?> existing = registry.get("item-frame-interact");
if (existing instanceof StateFlag) {
FLAG_ITEM_FRAME_INTERACT = (StateFlag) existing;
}
}
}
@Override
public void onEnable() {
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
regionQuery = container.createQuery();
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler(ignoreCancelled = true)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof ItemFrame)) return;
ItemFrame itemFrame = (ItemFrame) event.getEntity();
if (itemFrame.getItem().getType() != Material.AIR) {
// entity is trying to remove the item from the frame
if (!regionQuery.testState(BukkitAdapter.adapt(itemFrame.getLocation()), null, FLAG_ITEM_FRAME_INTERACT)) {
// flag is denied in this region, not allowed
event.setCancelled(true);
}
} else {
// entity is trying to break the item frame, worldguard will handle this via entity-item-frame-destroy flag
}
}
@EventHandler(ignoreCancelled = true)
public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent event) {
if (!(event.getRightClicked() instanceof ItemFrame)) return;
ItemFrame itemFrame = (ItemFrame) event.getRightClicked();
Location location = BukkitAdapter.adapt(itemFrame.getLocation());
if (!regionQuery.testState(location, null, FLAG_ITEM_FRAME_INTERACT)) {
event.setCancelled(true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment