Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply item mods to across modules #933

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Pugzy marked this conversation as resolved.
Show resolved Hide resolved

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
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);
}
}
}
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));
Pugzy marked this conversation as resolved.
Show resolved Hide resolved

// 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));
Comment on lines +68 to +69
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal of ItemModifyModule is to ensure that all ItemStacks are modified to certain rules, I would think that parseItem should return the already modified item? The problem here is that the caller has to remember to call applyRules, which would be quite easy to forget.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original pgm did it like this and i don't know why, but something tells me it's better not to.

I feel there may be use-cases for parseItem that don't require itemmodding

items.add(itemStack);
}

Filter filter =
Expand Down
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();
}
}