Skip to content

Commit

Permalink
added switching for placing blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
kristianperkins committed Sep 5, 2012
1 parent f168aa6 commit 0f0ce70
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,34 @@
import java.util.Map;
import java.util.logging.Logger;

import org.bukkit.Material;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.Plugin;

import com.github.krockode.itemswitcher.util.SwitcherStatus;

public class ItemSwitcherPlayerListener implements Listener {

private static final String MATCHERS_CONFIG_FILE = "/com/github/krockode/itemswitcher/matchers.yml";
private final Plugin plugin;
private final Logger log;
// item in hand to enable switching
private final String enableSwitchingRegex;
private final List<ItemSwitcherBlockMatcher> blockMatchers;
private final Map<String, SwitcherStatus> enabledPlayers;

public ItemSwitcherPlayerListener(final Plugin plugin, Map<String, SwitcherStatus> enabledPlayers) {
this.plugin = plugin;
this.log = plugin.getLogger();
Configuration configuration = plugin.getConfig();
this.enabledPlayers = enabledPlayers;
Expand Down Expand Up @@ -69,6 +76,49 @@ public void onPlayerInteract(final PlayerInteractEvent event) {
}
}

// Handle block place switching
// runs on high priority to let others cancel event
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
public void onItemPlace(final BlockPlaceEvent event) {
if (!enabledPlayers.keySet().contains(event.getPlayer().getName())) {
return;
}
if (!event.isCancelled() && event.getItemInHand().getAmount() == 1) {
Player player = event.getPlayer();
BlockSwitcher blockSwitcher = new BlockSwitcher(player, player.getInventory().getHeldItemSlot(), event.getItemInHand().getType());
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, blockSwitcher);
}
}

private class BlockSwitcher implements Runnable {

private Player player;
private int itemIndex;
private Material type;

public BlockSwitcher(Player player, int itemIndex, Material type) {
this.player = player;
this.itemIndex = itemIndex;
this.type = type;
}

public void run() {
PlayerInventory inventory = player.getInventory();
ItemStack itemInHand = player.getItemInHand();
if ((inventory.getHeldItemSlot() == itemIndex) && itemInHand.getType() == Material.AIR) {
ItemStack[] items = inventory.getContents();
for (int i = 0; i < items.length; i++) {
ItemStack item = items[i];
if (item != null && item.getType() == type) {
player.setItemInHand(item);
inventory.setItem(i, itemInHand);
break;
}
}
}
}
}

// Specific Handlers to revert switched items

@EventHandler(ignoreCancelled = true)
Expand Down

0 comments on commit 0f0ce70

Please sign in to comment.