From ba44d72609574ed36b776dad556c1518e4431bc0 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Sat, 3 Mar 2012 11:02:15 +0100 Subject: [PATCH] some metadata implementation for blocking drops of creative placed blocks --- config.yml | 3 + .../minecraft/limitedcreative/Core.java | 8 ++- .../listeners/CreativeBlockListener.java | 66 ++++++++++++++++++ .../listeners/GenericListener.java | 68 +++++++++++++++++++ .../listeners/InventoryStoreListener.java | 54 +++++++++++++++ .../listeners/LimitListener.java | 2 +- 6 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/listeners/CreativeBlockListener.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/listeners/GenericListener.java create mode 100644 src/de/jaschastarke/minecraft/limitedcreative/listeners/InventoryStoreListener.java diff --git a/config.yml b/config.yml index d593bf5..1e77589 100644 --- a/config.yml +++ b/config.yml @@ -121,6 +121,9 @@ limit: break: - bedrock +blocks: + enabled: true + region: # RegionsEnabled # Enables the Feature for "creative-regions". This Feature is automatically disabled, if the required plugin diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Core.java b/src/de/jaschastarke/minecraft/limitedcreative/Core.java index 9dcc317..e08658c 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/Core.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/Core.java @@ -25,8 +25,10 @@ import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; import de.jaschastarke.minecraft.integration.Communicator; +import de.jaschastarke.minecraft.limitedcreative.listeners.CreativeBlockListener; +import de.jaschastarke.minecraft.limitedcreative.listeners.GenericListener; import de.jaschastarke.minecraft.limitedcreative.listeners.LimitListener; -import de.jaschastarke.minecraft.limitedcreative.listeners.MainListener; +import de.jaschastarke.minecraft.limitedcreative.listeners.InventoryStoreListener; import de.jaschastarke.minecraft.limitedcreative.regions.WorldGuardIntegration; import de.jaschastarke.minecraft.utils.Locale; import de.jaschastarke.minecraft.utils.Permissions; @@ -73,7 +75,9 @@ public class Core extends JavaPlugin { warn(L("basic.conflict", "MultiInv", L("basic.feature.store"))); config.setTempStoreEnabled(false); } - getServer().getPluginManager().registerEvents(new MainListener(this), this); + getServer().getPluginManager().registerEvents(new InventoryStoreListener(this), this); + getServer().getPluginManager().registerEvents(new GenericListener(this), this); + getServer().getPluginManager().registerEvents(new CreativeBlockListener(this), this); // 2nd Feature: Creative Limitations (Restrictions) if (config.getLimitEnabled()) diff --git a/src/de/jaschastarke/minecraft/limitedcreative/listeners/CreativeBlockListener.java b/src/de/jaschastarke/minecraft/limitedcreative/listeners/CreativeBlockListener.java new file mode 100644 index 0000000..8e532b5 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/listeners/CreativeBlockListener.java @@ -0,0 +1,66 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.listeners; + +import java.util.List; + +import org.bukkit.GameMode; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.metadata.FixedMetadataValue; +import org.bukkit.metadata.MetadataValue; + +import de.jaschastarke.minecraft.limitedcreative.Core; + +public class CreativeBlockListener implements Listener { + private Core plugin; + public CreativeBlockListener(Core plugin) { + this.plugin = plugin; + } + + @EventHandler(ignoreCancelled=true) + public void onBlockBreak(BlockBreakEvent event) { + if (event.getPlayer().getGameMode() != GameMode.CREATIVE) { + if (event.getBlock().hasMetadata("created_gamemode")) { + List metadata = event.getBlock().getMetadata("created_gamemode"); + for (MetadataValue m : metadata) { + if (m.getOwningPlugin().equals(plugin)) { + if (GameMode.valueOf(m.asString()) == GameMode.CREATIVE) { + plugin.spawnblock.block(event.getBlock()); + } + } + } + } + } + } + + @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true) + public void onBlockPlace(BlockPlaceEvent event) { + MetadataValue test = new FixedMetadataValue(plugin, event.getPlayer().getGameMode().name()); + Core.debug("test: " + test.value().toString() + " - " + test.asString()); + + event.getBlock().setMetadata("created_by", new FixedMetadataValue(plugin, event.getPlayer().getName())); + event.getBlock().setMetadata("created_gamemode", new FixedMetadataValue(plugin, event.getPlayer().getGameMode().name())); + event.getBlock().setMetadata("created_at", new FixedMetadataValue(plugin, System.currentTimeMillis())); + + Core.debug(event.getBlock() + ": metadata created_gamemode: " + event.getBlock().getMetadata("created_gamemode").get(0).value().toString()); + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/listeners/GenericListener.java b/src/de/jaschastarke/minecraft/limitedcreative/listeners/GenericListener.java new file mode 100644 index 0000000..b81734a --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/listeners/GenericListener.java @@ -0,0 +1,68 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.listeners; + +import org.bukkit.entity.Item; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.ItemSpawnEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerQuitEvent; + +import de.jaschastarke.minecraft.limitedcreative.Core; +import de.jaschastarke.minecraft.limitedcreative.Players; + +public class GenericListener implements Listener { + private Core plugin; + public GenericListener(Core plugin) { + this.plugin = plugin; + } + + /** + * Needed by 3 of 4 features. + * - Creative Limits + * - Creative Blocks in Survival + * - Regions + */ + @EventHandler + public void onItemSpawn(ItemSpawnEvent event) { + if (event.isCancelled()) + return; + if (event.getEntity() instanceof Item) { + if (plugin.spawnblock.isBlocked(event.getLocation().getBlock().getLocation(), ((Item) event.getEntity()).getItemStack().getType())) { + event.setCancelled(true); + } + } + } + + public void onLogout(PlayerQuitEvent event) { + // what? i can't cancel a logout event? but how to chain the user to the server than? xD + Players.remove(event.getPlayer().getName()); + } + + /** + * The isCancelled in PlayerInteractEvent doesn't check useItemInHand, even this decides (when clicking on + * entity with e.g. a bucket) + * @param event + * @return The relevant "isCancelled" + */ + public static boolean isCancelled(PlayerInteractEvent event) { + return event.useInteractedBlock() == Event.Result.DENY && event.useItemInHand() == Event.Result.DENY; + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/listeners/InventoryStoreListener.java b/src/de/jaschastarke/minecraft/limitedcreative/listeners/InventoryStoreListener.java new file mode 100644 index 0000000..4e41a15 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/listeners/InventoryStoreListener.java @@ -0,0 +1,54 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerGameModeChangeEvent; +import org.bukkit.event.player.PlayerRespawnEvent; + +import de.jaschastarke.minecraft.limitedcreative.Core; +import de.jaschastarke.minecraft.limitedcreative.Players; + +public class InventoryStoreListener implements Listener { + private Core plugin; + public InventoryStoreListener(Core plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) { + if (Core.isDebug()) { + Core.debug("onPlayerGameModeChange: "+event.getPlayer().getName()); + Core.debug("Current GameMode: "+event.getPlayer().getGameMode()); + Core.debug("New GameMode: "+event.getNewGameMode()); + Core.debug("isLoggedin: "+plugin.com.isLoggedIn(event.getPlayer())); + Core.debug("isCancelled: "+event.isCancelled()); + } + if (!plugin.com.isLoggedIn(event.getPlayer())) + return; + + if (!Players.get(event.getPlayer()).onSetGameMode(event.getNewGameMode())) + event.setCancelled(true); + } + @EventHandler + public void onPlayerRespawn(PlayerRespawnEvent event) { + Players.get(event.getPlayer()).onRevive(); + } + +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/listeners/LimitListener.java b/src/de/jaschastarke/minecraft/limitedcreative/listeners/LimitListener.java index 14139d1..00e8e10 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/listeners/LimitListener.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/listeners/LimitListener.java @@ -72,7 +72,7 @@ public class LimitListener implements Listener { @EventHandler(priority=EventPriority.LOWEST) public void onPlayerInteract(PlayerInteractEvent event) { - if (MainListener.isCancelled(event) || event.getPlayer().getGameMode() != GameMode.CREATIVE) + if (GenericListener.isCancelled(event) || event.getPlayer().getGameMode() != GameMode.CREATIVE) return; LCPlayer player = Players.get(event.getPlayer());