Skip to content
This repository has been archived by the owner on Nov 25, 2019. It is now read-only.

Team Chest Mutation #90

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Conversation

DirkyJerky
Copy link

No description provided.

import tc.oc.pgm.mutation.types.KitMutation;
import tc.oc.pgm.wool.WoolMatchModule;

import java.util.*;

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;

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?

Copy link
Author

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));

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?

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?

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;

Choose a reason for hiding this comment

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

Will this actually happen?

Copy link
Author

@DirkyJerky DirkyJerky Jun 9, 2018

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);

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) {

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?

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?

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

Choose a reason for hiding this comment

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

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 -> {

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) {

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.

Copy link

@Electroid Electroid left a 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));

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";

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)

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) {

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());

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<>();

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;

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()) {

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();

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));

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.

Copy link
Author

@DirkyJerky DirkyJerky Jun 9, 2018

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

@DirkyJerky
Copy link
Author

DirkyJerky commented Jun 9, 2018

I updated things as per review.

I suggest using github's "Squash as you merge" feature here :P.

The ignoreCancelled = true is preventing the event handler from receiving any RIGHT_CLICK_AIR events, so should I remove the ignoreCancelled or just forgo right clicking air opening the chest?


private Kit getKitForPlayer(MatchPlayer player) {
ItemStack stack = new ItemBuilder(item(TOOL_TYPE))
.name(ChatColor.DARK_PURPLE + PGMTranslations.t("mutation.type.teamchest.item_name", player))

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?

Copy link
Author

@DirkyJerky DirkyJerky Jun 10, 2018

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()

Choose a reason for hiding this comment

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

toLegacyText()

Copy link
Author

@DirkyJerky DirkyJerky Jun 10, 2018

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.

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();
}

Choose a reason for hiding this comment

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

Remember super.disable()

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?

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))

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.

@Electroid
Copy link

You can forgo the ignoreCancelled and this is ready to ship after some minor fixes.

@Override
public void disable() {
teamChests.clear();
}

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.

@AustinLMayes
Copy link

@DirkyJerky any news?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants