Skip to content

Commit

Permalink
Much more elegant way to disable power-II potions.
Browse files Browse the repository at this point in the history
The glowstone refuses to be placed in the slot.

Closes #1.
  • Loading branch information
AmauryCarrade committed Aug 11, 2014
1 parent 040b18d commit db86938
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 29 deletions.
46 changes: 23 additions & 23 deletions src/main/java/me/azenet/UHPlugin/UHPluginListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;

import me.azenet.UHPlugin.i18n.I18n;
import me.azenet.UHPlugin.task.CancelBrewTask;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
Expand Down Expand Up @@ -34,6 +35,7 @@
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.BrewEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemConsumeEvent;
Expand Down Expand Up @@ -386,11 +388,15 @@ public void onPreCraftEvent(PrepareItemCraftEvent ev) {


/**
* Used to prevent an apple to be renamed to/from the name of an head apple.
* (In vanilla clients, it is not possible to rename an apple to that name because of the
* ChatColor.RESET before, but some modded clients allows the player to write §r.)
*
* (Thanks to Zelnehlun on BukkitDev.)
* Used to...
* - Prevent an apple to be renamed to/from the name of an head apple.
*
* (In vanilla clients, it is not possible to rename an apple to that name because of the
* ChatColor.RESET before, but some modded clients allows the player to write §r.)
*
* (Thanks to Zelnehlun on BukkitDev.)
*
* - Disable power-II potions.
*
* @param ev
*/
Expand Down Expand Up @@ -450,34 +456,28 @@ public void onInventoryClick(InventoryClickEvent ev) {
}
}
}

else if(ev.getInventory() instanceof BrewerInventory) {
if(p.getConfig().getBoolean("gameplay-changes.disableLevelIIPotions")) {
BukkitRunnable cancelBrewTask = new CancelBrewTask((BrewerInventory) ev.getInventory(), ev.getWhoClicked());
cancelBrewTask.runTaskLater(p, 1l);
}
}
}
}
}


/**
* Used to disable power-II potions.
*
* TODO find a better way to do this, by simulating the same behavior as when the player
* tries to brew a potion that doesn't exists.
*
* @param ev
*/
@EventHandler
public void onBrew(BrewEvent ev) {
@EventHandler(priority = EventPriority.MONITOR)
public void onInventoryDrag(InventoryDragEvent ev) {
if(p.getConfig().getBoolean("gameplay-changes.disableLevelIIPotions")) {
BrewerInventory brewerContent = ev.getContents();
ItemStack brewerInvContent = brewerContent.getIngredient();

if(brewerInvContent.getType().equals(Material.GLOWSTONE_DUST)) {

ev.setCancelled(true);

for(HumanEntity player : brewerContent.getViewers()) {
if(player instanceof Player) {
((Player) player).sendMessage(i.t("potions.disabled"));
}
}
}
BukkitRunnable cancelBrewTask = new CancelBrewTask((BrewerInventory) ev.getInventory(), ev.getWhoClicked());
cancelBrewTask.runTaskLater(p, 1l);
}
}

Expand Down
67 changes: 67 additions & 0 deletions src/main/java/me/azenet/UHPlugin/task/CancelBrewTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package me.azenet.UHPlugin.task;

import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.BrewerInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

public class CancelBrewTask extends BukkitRunnable {

private BrewerInventory inventory = null;
private HumanEntity whoClicked = null;

public CancelBrewTask(BrewerInventory inventory, HumanEntity whoClicked) {
this.inventory = inventory;
this.whoClicked = whoClicked;
}

@Override
public void run() {
if(inventory.getIngredient() == null) {
return; // Nothing to do!
}

if(whoClicked instanceof Player) {
ItemStack ingredient = inventory.getIngredient();

if(ingredient.getType() != null && ingredient.getType().equals(Material.GLOWSTONE_DUST)) {
inventory.setIngredient(new ItemStack(Material.AIR)); // The glowstone is removed.

// First try: try to add the glowstone to an existing stack
Boolean added = false;
for(ItemStack item : whoClicked.getInventory().getContents()) {
if(item != null && item.getType() != null && item.getType().equals(Material.GLOWSTONE_DUST)) {
if(item.getAmount() + ingredient.getAmount() <= item.getMaxStackSize()) {
// We can add the glowstone here.
item.setAmount(item.getAmount() + ingredient.getAmount());
added = true;
break;
}
}
}

if(!added) {
// Failed... We adds the glowstone to the first empty slot found.
added = false;

Integer slotEmpty = whoClicked.getInventory().firstEmpty();

if(slotEmpty != -1) { // -1 is returned if there isn't any enpty slot
whoClicked.getInventory().setItem(slotEmpty, ingredient);
}
else {
// Failed again (!). Maybe an item captured between the click and this execution.
// The stack is dropped at the player's location.
whoClicked.getWorld().dropItem(whoClicked.getLocation(), ingredient);
}
}

((Player) whoClicked).updateInventory();
}
}
}

}
3 changes: 0 additions & 3 deletions src/main/resources/i18n/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ keys:
nameGoldenAppleFromHeadNormal: "{aqua}Golden head"
nameGoldenAppleFromHeadNotch: "{lightpurple}Golden head"

potions:
disabled: "{ce}These potions are disabled!"

compass:
noRottenFlesh: "{gray}{italic}You do not have rotten flesh."
nothingFound: "{gray}{italic}Only silence answers your request."
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/i18n/fr_FR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ keys:
nameGoldenAppleFromHeadNormal: "{aqua}Tête tranchée"
nameGoldenAppleFromHeadNotch: "{lightpurple}Tête tranchée"

potions:
disabled: "{ce}Ces potions sont désactivées !"

compass:
noRottenFlesh: "{gray}{italic}Vous n'avez pas de chair de zombie."
nothingFound: "{gray}{italic}Seul le silence comble votre requête."
Expand Down

0 comments on commit db86938

Please sign in to comment.