-
Notifications
You must be signed in to change notification settings - Fork 13
Team Chest Mutation #90
base: master
Are you sure you want to change the base?
Changes from 2 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,135 @@ | ||
| package tc.oc.pgm.mutation.types.kit; | ||
|
|
||
| import org.bukkit.ChatColor; | ||
| import org.bukkit.Material; | ||
| 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.*; | ||
|
|
||
| public class TeamChestMutation extends KitMutation { | ||
| final static int SLOT_ID = 17; // Top right | ||
| final static Material TOOL_TYPE = Material.ENDER_CHEST; | ||
|
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. Do we need this extra field?
Author
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. Because it is used six times, I believe so. So in the future it may be easy to change it if needed. |
||
| final static String ITEM_NAME_KEY = "mutation.type.teamchest.item_name"; | ||
|
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. The translation strings don't need to be statically defined |
||
| final static String ITEM_LORE_KEY = "mutation.type.teamchest.item_lore"; | ||
| 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> oWmm; | ||
|
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. Just call it |
||
|
|
||
| public TeamChestMutation(Match match) { | ||
| super(match, false); | ||
| oWmm = Optional.ofNullable(match().getMatchModule(WoolMatchModule.class)); | ||
|
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. If you want the optional use |
||
| 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 kits(MatchPlayer player, List<Kit> kits) { | ||
| super.kits(player, kits); | ||
| kits.add(getKitForPlayer(player)); | ||
| } | ||
|
|
||
| // Open shared inventory instead of placing the chest | ||
| @EventHandler(priority = EventPriority.HIGHEST) | ||
|
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. Add to the annotation |
||
| 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;
} |
||
| if (event.getItem() == null) return; | ||
|
|
||
| if (!(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) return; | ||
| if (event.getItem().getType() != TOOL_TYPE) return; | ||
|
|
||
| Player bukkitPlayer = event.getPlayer(); | ||
| if (bukkitPlayer == null) return; | ||
|
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. Will this actually happen?
Author
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 don't know. I've seen some weird things with Bukkit events returning null when they shouldn't be, better safe than sorry. |
||
| Optional<Inventory> oTeamInventory = getTeamsInventory(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. If you want to prefix your optional variables use |
||
|
|
||
| oTeamInventory.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); | ||
|
|
||
|
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. Unnecessary newline |
||
| }); | ||
| } | ||
|
|
||
| @EventHandler(priority = EventPriority.LOWEST) | ||
| public void onInventoryClick(InventoryClickEvent event) { | ||
| if (event.getCurrentItem() == null) return; | ||
|
|
||
| // No putting evil items (ender chest, possibly wool) into the chest | ||
| Optional<Inventory> teamChest = getTeamsInventory(event.getActor()); | ||
| if (teamChest.filter(teamInventory -> { | ||
| return teamInventory.equals(event.getView().getTopInventory()); | ||
|
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. Single line lambdas don't need the if(getTeamsInventory(event.getActor()).filter(i -> i.equals(event.getView().getTopInventory())).isPresent() &&
isBlacklistedItem(event.getCurrentItem()) {
event.setCancelled(true);
return;
} |
||
| }).isPresent() && | ||
| isItemEvil(event.getCurrentItem())) { | ||
| event.setCancelled(true); | ||
| return; | ||
| } | ||
|
|
||
| // If normal right click, in their inventory, on the chest, then open shared inventory. | ||
| getTeamsInventory(event.getActor()).ifPresent(teamInventory -> { | ||
|
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. DRY, can we set a variable for |
||
| 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. | ||
| event.getActor().closeInventory(); | ||
|
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. Since you use |
||
| // Prevent visual inconsistencies, specifically off-hand slot | ||
| if (event.getSlotType() == InventoryType.SlotType.QUICKBAR) { | ||
| event.getActor().updateInventory(); | ||
| } | ||
| event.getActor().openInventory(teamInventory); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private boolean isItemEvil(ItemStack item) { | ||
|
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.
|
||
| if(item.getType() == TOOL_TYPE) return true; | ||
| if(oWmm.filter(wmm -> wmm.isObjectiveWool(item)).isPresent()) return true; | ||
|
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. Be careful here. If private boolean isBlacklistedItem(ItemStack item) {
return item.getType() == Material.ENDER_CHEST || wools.map(w -> w.isObjectiveWool(item)).orElse(false);
} |
||
|
|
||
| return 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 |
||
| MatchPlayer player = match().getPlayer(bukkitPlayer); | ||
| Party team = player.getParty(); | ||
|
|
||
| if (!team.isParticipating()) return Optional.empty(); | ||
|
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. Instead of a team check, you can check the player. If they aren't participating, it will be optional absent. |
||
|
|
||
| return Optional.of(teamChests.get(team)); | ||
|
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
Author
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. """ |
||
| } | ||
|
|
||
| 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(ITEM_NAME_KEY, player)) | ||
| .lore(ChatColor.DARK_AQUA + PGMTranslations.t(ITEM_LORE_KEY, player)) | ||
| .get(); | ||
|
|
||
| ItemKit kit = new SlotItemKit(stack, Slot.Player.forIndex(SLOT_ID)); | ||
|
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. You don't need the static variable but add a type comment here for what the |
||
| 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.
No wildcard imports please