-
Notifications
You must be signed in to change notification settings - Fork 14
Team Chest Mutation #90
base: master
Are you sure you want to change the base?
Changes from 17 commits
9c1d871
3c40914
bf6bd71
faad1cd
8b038f6
d2dac49
0edadff
839b4dc
f0e720b
261a032
8748f45
935b209
952e20b
da42262
a10f8e4
33863c3
2bf6472
9b27ca8
dcd7c1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package tc.oc.pgm.mutation.types.kit; | ||
|
||
import org.bukkit.ChatColor; | ||
import org.bukkit.Material; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.EventPriority; | ||
import org.bukkit.event.block.Action; | ||
import org.bukkit.event.inventory.InventoryAction; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.bukkit.event.inventory.InventoryType; | ||
import org.bukkit.event.player.PlayerInteractEvent; | ||
import org.bukkit.inventory.EquipmentSlot; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import tc.oc.commons.bukkit.inventory.Slot; | ||
import tc.oc.commons.bukkit.item.ItemBuilder; | ||
import tc.oc.pgm.PGMTranslations; | ||
import tc.oc.pgm.kits.ItemKit; | ||
import tc.oc.pgm.kits.Kit; | ||
import tc.oc.pgm.kits.SlotItemKit; | ||
import tc.oc.pgm.match.Match; | ||
import tc.oc.pgm.match.MatchPlayer; | ||
import tc.oc.pgm.match.Party; | ||
import tc.oc.pgm.mutation.types.KitMutation; | ||
import tc.oc.pgm.wool.WoolMatchModule; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.WeakHashMap; | ||
|
||
public class TeamChestMutation extends KitMutation { | ||
final static Material TOOL_TYPE = Material.ENDER_CHEST; | ||
final static int CHEST_SIZE = 27; | ||
|
||
final Map<Party, Inventory> teamChests = new WeakHashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even though this is weak, you want to clear this |
||
|
||
final Optional<WoolMatchModule> optWools; | ||
|
||
public TeamChestMutation(Match match) { | ||
super(match, false); | ||
optWools = match().module(WoolMatchModule.class); | ||
} | ||
|
||
@Override | ||
public void enable() { | ||
super.enable(); | ||
for (Party party : match().getParties()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mutation modules are special, because they can be enabled or disabled on the fly. So this logic should go into @Override
public boolean enable() {
super.enable();
for (Party party : match().getParties()) {
// Rest of implementation
}
} You will also have to make sure you implement |
||
if (party.isParticipatingType()) { | ||
// Could the chest title be localized properly? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it can, but you'll have to create an inventory view each time a player wishes to view it. See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make this a |
||
teamChests.put(party, match().getServer().createInventory(null, CHEST_SIZE)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void disable() { | ||
teamChests.clear(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remember There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's best to disable the current class, then the super class. |
||
|
||
@Override | ||
public void kits(MatchPlayer player, List<Kit> kits) { | ||
super.kits(player, kits); | ||
kits.add(getKitForPlayer(player)); | ||
} | ||
|
||
// Open shared inventory instead of placing the chest | ||
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) | ||
public void onChestUse(PlayerInteractEvent event) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rule of thumb for player events, very early in your event block you want to convert to a MatchPlayer player = match.participant(event.getPlayer());
if(!player.isPresent() ||
!(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) ||
event.getItem() == null || event.getItem().getType() != Material.ENDER_CHEST) {
return;
} |
||
Player bukkitPlayer = event.getPlayer(); | ||
Optional<MatchPlayer> optPlayer = match().participant((Entity) bukkitPlayer); | ||
if (!optPlayer.isPresent() || | ||
!(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) || | ||
event.getItem() == null || | ||
event.getItem().getType() != TOOL_TYPE) { | ||
return; | ||
} | ||
|
||
Optional<Inventory> optTeamInventory = getTeamsInventory(bukkitPlayer); | ||
optTeamInventory.ifPresent(teamInventory -> { | ||
event.setCancelled(true); | ||
// If the item is in the off-hand slot, it wont get put back visually for the player without this. | ||
if(event.getHand() == EquipmentSlot.OFF_HAND) event.getActor().updateInventory(); | ||
bukkitPlayer.openInventory(teamInventory); | ||
}); | ||
} | ||
|
||
@EventHandler(priority = EventPriority.LOWEST) | ||
public void onInventoryClick(InventoryClickEvent event) { | ||
Player player = event.getActor(); | ||
if (event.getCurrentItem() == null) return; | ||
|
||
// No putting blacklisted items (ender chest, possibly wool) into the chest | ||
Optional<Inventory> teamChest = getTeamsInventory(player); | ||
if (teamChest.map(teamInventory -> teamInventory.equals(event.getView().getTopInventory())).orElse(false) && | ||
isBlacklistedItem(event.getCurrentItem())) { | ||
event.setCancelled(true); | ||
return; | ||
} | ||
|
||
// If normal right click, in their inventory, on the chest, then open shared inventory. | ||
getTeamsInventory(player).ifPresent(teamInventory -> { | ||
if (event.getInventory().getType() == InventoryType.CRAFTING && | ||
event.getCurrentItem().getType() == TOOL_TYPE && | ||
event.getAction() == InventoryAction.PICKUP_HALF) { | ||
event.setCancelled(true); | ||
// This resets their mouse position annoyingly, but without it items can get stuck in places. | ||
player.closeInventory(); | ||
// Prevent visual inconsistencies, specifically off-hand slot | ||
if (event.getSlotType() == InventoryType.SlotType.QUICKBAR) { | ||
player.updateInventory(); | ||
} | ||
player.openInventory(teamInventory); | ||
} | ||
}); | ||
} | ||
|
||
private boolean isBlacklistedItem(ItemStack item) { | ||
return item.getType() == TOOL_TYPE || | ||
optWools.map(w -> w.isObjectiveWool(item)).orElse(false); | ||
} | ||
|
||
private Optional<Inventory> getTeamsInventory(Player bukkitPlayer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For our needs, would an @nullable possibly be better? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we take in a MatchPlayer instead of a bukkit Player object? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use |
||
return match().participant((Entity) bukkitPlayer) | ||
.map(matchPlayer -> teamChests.get(matchPlayer.getParty())); | ||
} | ||
|
||
private Kit getKitForPlayer(MatchPlayer player) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PGMTranslations is legacy. See |
||
ItemStack stack = new ItemBuilder(item(TOOL_TYPE)) | ||
.name(ChatColor.DARK_PURPLE + PGMTranslations.t("mutation.type.teamchest.item_name", player)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we able to not use PGMTranslations? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't seem to get it to work yet, Neither did There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. toLegacyText() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do I get a I can't inject it as I assume that is how it is normally recieved. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mutations don't support injection right now, so using |
||
.lore(ChatColor.DARK_AQUA + PGMTranslations.t("mutation.type.teamchest.item_lore", player)) | ||
.get(); | ||
|
||
ItemKit kit = new SlotItemKit(stack, Slot.Player.forIndex(17)); | ||
return kit; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this extra field?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is used six times, I believe so. So in the future it may be easy to change it if needed.