Skip to content

Commit

Permalink
Apply item mods to across modules
Browse files Browse the repository at this point in the history
Signed-off-by: Pugzy <[email protected]>
  • Loading branch information
Pugzy committed Nov 4, 2021
1 parent 4bdfa28 commit 685c550
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
11 changes: 8 additions & 3 deletions core/src/main/java/tc/oc/pgm/blockdrops/BlockDropsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
Expand Down Expand Up @@ -44,7 +45,8 @@ public MatchModule createMatchModule(Match match) {
public static class Factory implements MapModuleFactory<BlockDropsModule> {
@Override
public Collection<Class<? extends MapModule>> getWeakDependencies() {
return ImmutableList.of(KitModule.class, RegionModule.class, FilterModule.class);
return ImmutableList.of(
KitModule.class, RegionModule.class, FilterModule.class, ItemModifyModule.class);
}

@Override
Expand All @@ -53,6 +55,8 @@ public BlockDropsModule parse(MapFactory factory, Logger logger, Document doc)
List<BlockDropsRule> rules = new ArrayList<>();
FilterParser filterParser = factory.getFilters();
RegionParser regionParser = factory.getRegions();
final Optional<ItemModifyModule> itemModifier =
Optional.ofNullable(factory.getModule(ItemModifyModule.class));

for (Element elRule :
XMLUtils.flattenElements(
Expand Down Expand Up @@ -85,9 +89,10 @@ public BlockDropsModule parse(MapFactory factory, Logger logger, Document doc)
Map<ItemStack, Double> items = new LinkedHashMap<>();
for (Element elDrops : elRule.getChildren("drops")) {
for (Element elItem : elDrops.getChildren("item")) {
final ItemStack itemStack = factory.getKits().parseItem(elItem, false);
itemModifier.ifPresent(imm -> imm.applyRules(itemStack));
items.put(
factory.getKits().parseItem(elItem, false),
XMLUtils.parseNumber(elItem.getAttribute("chance"), Double.class, 1d));
itemStack, XMLUtils.parseNumber(elItem.getAttribute("chance"), Double.class, 1d));
}
}

Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/tc/oc/pgm/itemmeta/ItemModifyMatchModule.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package tc.oc.pgm.itemmeta;

import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.match.MatchModule;
import tc.oc.pgm.api.match.MatchScope;
import tc.oc.pgm.events.ListenerScope;
import tc.oc.pgm.util.nms.NMSHacks;

@ListenerScope(MatchScope.LOADED)
public class ItemModifyMatchModule implements MatchModule, Listener {
Expand Down Expand Up @@ -69,4 +72,19 @@ public void onArmorDispense(BlockDispenseEvent event) {
event.setItem(stack);
}
}

@EventHandler(ignoreCancelled = true)
public void onItemPickup(PlayerPickupItemEvent event) {
// Needed for players picking up arrows stuck in blocks
if (!NMSHacks.isCraftItemArrowEntity(event.getItem())) return;

final Item item = event.getItem();
final ItemStack itemStack = item.getItemStack();

if (applyRules(itemStack)) {
event.setCancelled(true);
NMSHacks.fakePlayerItemPickup(event.getPlayer(), item);
event.getPlayer().getInventory().addItem(itemStack);
}
}
}
9 changes: 7 additions & 2 deletions core/src/main/java/tc/oc/pgm/killreward/KillRewardModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.logging.Logger;
import org.bukkit.inventory.ItemStack;
import org.jdom2.Document;
Expand Down Expand Up @@ -40,7 +41,7 @@ public MatchModule createMatchModule(Match match) {
public static class Factory implements MapModuleFactory<KillRewardModule> {
@Override
public Collection<Class<? extends MapModule>> getWeakDependencies() {
return ImmutableList.of(RegionModule.class, FilterModule.class);
return ImmutableList.of(RegionModule.class, FilterModule.class, ItemModifyModule.class);
}

@Override
Expand All @@ -52,6 +53,8 @@ public Collection<Class<? extends MapModule>> getSoftDependencies() {
public KillRewardModule parse(MapFactory factory, Logger logger, Document doc)
throws InvalidXMLException {
ImmutableList.Builder<KillReward> rewards = ImmutableList.builder();
final Optional<ItemModifyModule> itemModifier =
Optional.ofNullable(factory.getModule(ItemModifyModule.class));

// Must allow top-level children for legacy support
for (Element elKillReward :
Expand All @@ -62,7 +65,9 @@ public KillRewardModule parse(MapFactory factory, Logger logger, Document doc)
0)) {
ImmutableList.Builder<ItemStack> items = ImmutableList.builder();
for (Element itemEl : elKillReward.getChildren("item")) {
items.add(factory.getKits().parseItem(itemEl, false));
final ItemStack itemStack = factory.getKits().parseItem(itemEl, false);
itemModifier.ifPresent(imm -> imm.applyRules(itemStack));
items.add(itemStack);
}

Filter filter =
Expand Down
13 changes: 7 additions & 6 deletions core/src/main/java/tc/oc/pgm/modules/ToolRepairMatchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Arrays;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
Expand All @@ -15,6 +15,7 @@
import tc.oc.pgm.api.match.MatchModule;
import tc.oc.pgm.api.match.MatchScope;
import tc.oc.pgm.events.ListenerScope;
import tc.oc.pgm.util.nms.NMSHacks;

@ListenerScope(MatchScope.RUNNING)
public class ToolRepairMatchModule implements MatchModule, Listener {
Expand All @@ -31,14 +32,14 @@ private boolean canRepair(ItemStack pickup, ItemStack invStack) {
}

private void doRepair(PlayerPickupItemEvent event, ItemStack stack) {
ItemStack pickup = event.getItem().getItemStack();
Item item = event.getItem();
ItemStack pickup = item.getItemStack();

event.setCancelled(true);
NMSHacks.fakePlayerItemPickup(event.getPlayer(), item);

int hitsLeft = pickup.getType().getMaxDurability() - pickup.getDurability() + 1;
stack.setDurability((short) Math.max(stack.getDurability() - hitsLeft, 0));

event.setCancelled(true);
event.getItem().remove();
event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ITEM_PICKUP, 0.5f, 1f);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand Down
14 changes: 14 additions & 0 deletions util/src/main/java/tc/oc/pgm/util/nms/NMSHacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -1333,4 +1333,18 @@ static void skipFireworksLaunch(Firework firework) {
setFireworksTicksFlown(firework, 2);
sendEntityMetadataToViewers(firework, false);
}

static boolean isCraftItemArrowEntity(org.bukkit.entity.Item item) {
return ((CraftItem) item).getHandle() instanceof EntityArrow;
}

static void fakePlayerItemPickup(Player player, org.bukkit.entity.Item item) {
float pitch = (((float) (Math.random() - Math.random()) * 0.7F + 1.0F) * 2.0F);
item.getWorld().playSound(item.getLocation(), org.bukkit.Sound.ITEM_PICKUP, 0.2F, pitch);

NMSHacks.sendPacketToViewers(
item, new PacketPlayOutCollect(item.getEntityId(), player.getEntityId()));

item.remove();
}
}

0 comments on commit 685c550

Please sign in to comment.