Conversation
| 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.
Because it is used six times, I believe so. So in the future it may be easy to change it if needed.
|
|
||
| public TeamChestMutation(Match match) { | ||
| super(match, false); | ||
| oWmm = Optional.ofNullable(match().getMatchModule(WoolMatchModule.class)); |
There was a problem hiding this comment.
Match::getMatchModule is an @nullable. Is there a need for an Optional?
There was a problem hiding this comment.
If you want the optional use match().module(...)
| oWmm = Optional.ofNullable(match().getMatchModule(WoolMatchModule.class)); | ||
| for (Party party : match().getParties()) { | ||
| if (party.isParticipatingType()) { | ||
| // Could the chest title be localized properly? |
There was a problem hiding this comment.
Yes it can, but you'll have to create an inventory view each time a player wishes to view it. See NavigatorInterface.
| if (event.getItem().getType() != TOOL_TYPE) return; | ||
|
|
||
| Player bukkitPlayer = event.getPlayer(); | ||
| if (bukkitPlayer == null) return; |
There was a problem hiding this comment.
I don't know. I've seen some weird things with Bukkit events returning null when they shouldn't be, better safe than sorry.
| // 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); | ||
|
|
| return false; | ||
| } | ||
|
|
||
| private Optional<Inventory> getTeamsInventory(Player bukkitPlayer) { |
There was a problem hiding this comment.
For our needs, would an @nullable possibly be better?
There was a problem hiding this comment.
Can we take in a MatchPlayer instead of a bukkit Player object?
There was a problem hiding this comment.
Optional's are preferred over null values
There was a problem hiding this comment.
Use Optionals if it makes for cleaner code, if it makes things messier then you don't need to.
| } | ||
|
|
||
| // 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.
DRY, can we set a variable for event.getActor()?
| return Optional.of(teamChests.get(team)); | ||
| } | ||
|
|
||
| private Kit getKitForPlayer(MatchPlayer player) { |
There was a problem hiding this comment.
PGMTranslations is legacy. See RenderedItemBuilder.
Electroid
left a comment
There was a problem hiding this comment.
Great work, Dirky!
There are a couple of odds and ends to fix, but overall you're doing a great job. In general, simplicity in code is a must, so as you learn the codebase you'll find we have lots of utility methods to make your life as a programmer easier.
Let me know if you have any questions.
|
|
||
| public TeamChestMutation(Match match) { | ||
| super(match, false); | ||
| oWmm = Optional.ofNullable(match().getMatchModule(WoolMatchModule.class)); |
There was a problem hiding this comment.
If you want the optional use match().module(...)
| public class TeamChestMutation extends KitMutation { | ||
| final static int SLOT_ID = 17; // Top right | ||
| final static Material TOOL_TYPE = Material.ENDER_CHEST; | ||
| final static String ITEM_NAME_KEY = "mutation.type.teamchest.item_name"; |
There was a problem hiding this comment.
The translation strings don't need to be statically defined
| } | ||
|
|
||
| // Open shared inventory instead of placing the chest | ||
| @EventHandler(priority = EventPriority.HIGHEST) |
There was a problem hiding this comment.
Add to the annotation ignoreCancelled = true
| }); | ||
| } | ||
|
|
||
| private boolean isItemEvil(ItemStack item) { |
| // 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.
Single line lambdas don't need the {} this whole block can be simplified:
if(getTeamsInventory(event.getActor()).filter(i -> i.equals(event.getView().getTopInventory())).isPresent() &&
isBlacklistedItem(event.getCurrentItem()) {
event.setCancelled(true);
return;
}| 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.
Even though this is weak, you want to clear this disable()
|
|
||
| final Map<Party, Inventory> teamChests = new WeakHashMap<>(); | ||
|
|
||
| final Optional<WoolMatchModule> oWmm; |
There was a problem hiding this comment.
Just call it wools or optWools, oWmm is ambiguous.
| public TeamChestMutation(Match match) { | ||
| super(match, false); | ||
| oWmm = Optional.ofNullable(match().getMatchModule(WoolMatchModule.class)); | ||
| for (Party party : match().getParties()) { |
There was a problem hiding this comment.
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 disable() too.
| MatchPlayer player = match().getPlayer(bukkitPlayer); | ||
| Party team = player.getParty(); | ||
|
|
||
| if (!team.isParticipating()) return Optional.empty(); |
There was a problem hiding this comment.
Instead of a team check, you can check the player.
MatchPlayer player = match().participant(bukkitPlayer)
If they aren't participating, it will be optional absent.
|
|
||
| if (!team.isParticipating()) return Optional.empty(); | ||
|
|
||
| return Optional.of(teamChests.get(team)); |
There was a problem hiding this comment.
Can teamChests.get(team) be null? If so you want Optional.ofNullable or else a runtime error will be thrown.
There was a problem hiding this comment.
"""
I don't think you need to throw an exception if a chest isn't found, can't think of any cases where that should happen.
"""
--- Shiny
|
I updated things as per review. I suggest using github's "Squash as you merge" feature here :P. The |
|
|
||
| private Kit getKitForPlayer(MatchPlayer player) { | ||
| 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.
Are we able to not use PGMTranslations?
There was a problem hiding this comment.
I couldn't seem to get it to work yet,
new Component(new TranslatableComponent("mutation.type.teamchest.item_name"), ChatColor.DARK_PURPLE).getText()
doesn't work.
Neither did new Component(ChatColor.DARK_PURPLE).translate("mutation.type.teamchest.item_name").getText()
There was a problem hiding this comment.
How do I get a ComponentRenderContext? That is what I assume I need (Readme says I need it), because nothing is translating it is just showing the translation key.
I can't inject it as I assume that is how it is normally recieved.
There was a problem hiding this comment.
Mutations don't support injection right now, so using PGMTranslations should be fine.
| @Override | ||
| public void disable() { | ||
| teamChests.clear(); | ||
| } |
There was a problem hiding this comment.
It's best to disable the current class, then the super class.
| super.enable(); | ||
| for (Party party : match().getParties()) { | ||
| if (party.isParticipatingType()) { | ||
| // Could the chest title be localized properly? |
|
|
||
| private Kit getKitForPlayer(MatchPlayer player) { | ||
| 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.
Mutations don't support injection right now, so using PGMTranslations should be fine.
|
You can forgo the |
| @Override | ||
| public void disable() { | ||
| teamChests.clear(); | ||
| } |
There was a problem hiding this comment.
It's best to disable the current class, then the super class.
|
@DirkyJerky any news? |
No description provided.