-
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?
Conversation
import tc.oc.pgm.mutation.types.KitMutation; | ||
import tc.oc.pgm.wool.WoolMatchModule; | ||
|
||
import java.util.*; |
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
|
||
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 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.
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Match::getMatchModule
is an @nullable. Is there a need for an Optional?
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.
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.
Choose a reason for hiding this comment
The 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 NavigatorInterface
.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Will this actually happen?
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.
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); | ||
|
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.
Unnecessary newline
return false; | ||
} | ||
|
||
private Optional<Inventory> getTeamsInventory(Player bukkitPlayer) { |
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.
For our needs, would an @nullable possibly be better?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Optional
's are preferred over null values
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.
Use Optional
s 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PGMTranslations is legacy. See RenderedItemBuilder
.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add to the annotation ignoreCancelled = true
}); | ||
} | ||
|
||
private boolean isItemEvil(ItemStack item) { |
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.
isBlacklistedItem
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The 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 disable()
too.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""
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.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember super.disable()
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.
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? |
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.
Make this a TODO
|
||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's best to disable the current class, then the super class.
@DirkyJerky any news? |
No description provided.