Skip to content

Commit

Permalink
Fixes protection for scooping tropical fish.
Browse files Browse the repository at this point in the history
#349

Also added pagination to protection flags and localized alphabetical
sorting of the flags.

Also added defensive code should a flag enum be removed but it is still
in the island database. In this case it will just be ignored and at the
next database object save it will disappear.
  • Loading branch information
tastybento committed Nov 21, 2018
1 parent 98264f0 commit ff29836
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void setup() {
public boolean execute(User user, String label, List<String> args) {
// Settings are only shown if you are in the right world
if (Util.getWorld(user.getWorld()).equals(getWorld())) {
SettingsPanel.openPanel(getPlugin(), user, Flag.Type.PROTECTION, getWorld());
SettingsPanel.openPanel(getPlugin(), user, Flag.Type.PROTECTION, getWorld(), 0);
return true;
} else {
user.sendMessage("general.errors.wrong-world");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ public Map<Flag, Integer> deserialize(Object object) {
if (object instanceof MemorySection) {
MemorySection section = (MemorySection) object;
for (String key : section.getKeys(false)) {
result.put(BentoBox.getInstance().getFlagsManager().getFlagByID(key), section.getInt(key));
Flag flag = BentoBox.getInstance().getFlagsManager().getFlagByID(key);
if (flag != null) {
result.put(flag, section.getInt(key));
}
}
} else {
for (Entry<String, Integer> en : ((Map<String, Integer>)object).entrySet()) {
result.put(BentoBox.getInstance().getFlagsManager().getFlagByID(en.getKey()), en.getValue());
Flag flag = BentoBox.getInstance().getFlagsManager().getFlagByID(en.getKey());
if (flag != null) {
result.put(flag, en.getValue());
}
}
}
return result;
Expand All @@ -48,7 +54,9 @@ public Map<String, Integer> serialize(Object object) {
}
Map<Flag, Integer> flags = (Map<Flag, Integer>)object;
for (Entry<Flag, Integer> en: flags.entrySet()) {
result.put(en.getKey().getID(), en.getValue());
if (en != null && en.getKey() != null) {
result.put(en.getKey().getID(), en.getValue());
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package world.bentobox.bentobox.listeners.flags;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.TropicalFish;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.event.player.PlayerBucketFillEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;

import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.lists.Flags;
Expand Down Expand Up @@ -37,6 +40,7 @@ public void onBucketEmpty(final PlayerBucketEmptyEvent e) {
*/
@EventHandler(priority = EventPriority.LOW)
public void onBucketFill(final PlayerBucketFillEvent e) {
Bukkit.getLogger().info("DEBUG: " + e.getEventName());
// Check filling of various liquids
if (e.getItemStack().getType().equals(Material.LAVA_BUCKET) && (!checkIsland(e, e.getBlockClicked().getLocation(), Flags.COLLECT_LAVA))) {
return;
Expand All @@ -51,4 +55,12 @@ public void onBucketFill(final PlayerBucketFillEvent e) {
checkIsland(e, e.getBlockClicked().getLocation(), Flags.BUCKET);
}

}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onTropicalFishSc(final PlayerInteractEntityEvent e) {
if (e.getRightClicked() instanceof TropicalFish && e.getPlayer().getInventory().getItemInMainHand().getType().equals(Material.WATER_BUCKET) && (!checkIsland(e, e.getRightClicked().getLocation(), Flags.FISH_SCOOPING))) {
return;
}
}


}
1 change: 1 addition & 0 deletions src/main/java/world/bentobox/bentobox/lists/Flags.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private Flags() {}
public static final Flag COLLECT_LAVA = new FlagBuilder().id("COLLECT_LAVA").icon(Material.LAVA_BUCKET).build();
public static final Flag COLLECT_WATER = new FlagBuilder().id("COLLECT_WATER").icon(Material.WATER_BUCKET).build();
public static final Flag MILKING = new FlagBuilder().id("MILKING").icon(Material.MILK_BUCKET).build();
public static final Flag FISH_SCOOPING = new FlagBuilder().id("FISH_SCOOPING").allowedByDefault(false).icon(Material.TROPICAL_FISH_BUCKET).build();

// Chorus Fruit and Enderpearls
public static final Flag CHORUS_FRUIT = new FlagBuilder().id("CHORUS_FRUIT").icon(Material.CHORUS_FRUIT).listener(new TeleportationListener()).build();
Expand Down
37 changes: 33 additions & 4 deletions src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package world.bentobox.bentobox.panels;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.bukkit.Material;
import org.bukkit.World;
Expand Down Expand Up @@ -29,7 +31,7 @@ private SettingsPanel() {}
* @param flagType - initial view
* @param world - world
*/
public static void openPanel(BentoBox plugin, User user, Flag.Type flagType, World world) {
public static void openPanel(BentoBox plugin, User user, Flag.Type flagType, World world, int page) {
String friendlyWorldName = plugin.getIWM().getFriendlyName(world);
// Create the panel
PanelBuilder panelBuilder = new PanelBuilder()
Expand All @@ -38,8 +40,35 @@ public static void openPanel(BentoBox plugin, User user, Flag.Type flagType, Wor

setupHeader(user, panelBuilder, flagType, world, friendlyWorldName);

plugin.getFlagsManager().getFlags().stream().filter(f -> f.getType().equals(flagType))
.sorted(Comparator.comparing(Flag::getID)).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
// Get a list of flags of the correct type and sort by the translated names
List<Flag> flags = plugin.getFlagsManager().getFlags().stream().filter(f -> f.getType().equals(flagType))
.sorted(Comparator.comparing(Flag::getID, (s1, s2) -> {
String s1Translation = user.getTranslation(plugin.getFlagsManager().getFlagByID(s1).getNameReference());
String s2Translation = user.getTranslation(plugin.getFlagsManager().getFlagByID(s2).getNameReference());
return s1Translation.compareTo(s2Translation);
}))
.collect(Collectors.toList());

// Use paging
flags.stream().skip(page * 43).limit(page * 43 + 43).forEach((f -> panelBuilder.item(f.toPanelItem(plugin, user))));
// Add forward and backward icons
if (page > 0) {
// Previous page icon
panelBuilder.item(new PanelItemBuilder().icon(Material.SIGN).name(user.getTranslation(PROTECTION_PANEL + "previous")).clickHandler((panel, user1, clickType, slot1) -> {
openPanel(BentoBox.getInstance(), user, flagType, world, page - 1);
return true;
}).build());
}
if ((page + 1) * 44 < flags.size()) {
// Next page icon
panelBuilder.item(new PanelItemBuilder().icon(Material.SIGN).name(user.getTranslation(PROTECTION_PANEL + "next")).clickHandler((panel, user1, clickType, slot1) -> {
openPanel(BentoBox.getInstance(), user, flagType, world, page + 1);
return true;
}).build());
}




// Show it to the player
panelBuilder.build().open(user);
Expand All @@ -55,7 +84,7 @@ private static void setupHeader(User user, PanelBuilder panelBuilder, Flag.Type
.glow(flagType.equals(currentFlagType))
.clickHandler((panel, user1, clickType, slot1) -> {
if (!flagType.equals(currentFlagType)) {
openPanel(BentoBox.getInstance(), user, flagType, world);
openPanel(BentoBox.getInstance(), user, flagType, world, 0);
}
return true;
})
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,12 @@ protection:
description: "Toggle spread"
name: "Fire spread"
hint: "No fire spread allowed"
FISH_SCOOPING:
description: |
&aAllow scooping of
&atropical fish
name: "Fish Scooping"
hint: "No scooping of tropical fish"
FURNACE:
description: "Toggle use"
name: "Furnace"
Expand Down Expand Up @@ -691,6 +697,8 @@ protection:
spawn-protected: "&cSpawn protected: [description]"

panel:
next: "Next Page"
previous: "Previous Page"
PROTECTION:
title: "Protection"
description: |-
Expand Down

0 comments on commit ff29836

Please sign in to comment.