Skip to content

Commit 3bac927

Browse files
committed
Fixed #14, island protections issues.
Added some new protection details: - PvP, Mob Attack, Animal Attack, Pickup/Drop Item, Collect/Fill lava. Notify players about their protection/settings update.
1 parent b1b5713 commit 3bac927

7 files changed

Lines changed: 124 additions & 94 deletions

File tree

src/main/java/com/larryTheCoder/ASkyBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public static void recheck() {
308308

309309
private void generateLevel() {
310310
database.fetchData(new QueryInfo("SELECT * FROM worldList"))
311-
.thenAccept(result -> {
311+
.whenComplete((result, error) -> {
312312
Map<Integer, String> levels = new HashMap<>();
313313

314314
result.rows().forEach(i -> levels.put(i.getInteger("levelId"), i.getString("worldName")));

src/main/java/com/larryTheCoder/ServerPanel.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import cn.nukkit.form.window.FormWindowModal;
4040
import cn.nukkit.form.window.FormWindowSimple;
4141
import cn.nukkit.level.biome.EnumBiome;
42+
import cn.nukkit.utils.TextFormat;
4243
import com.larryTheCoder.cache.IslandData;
4344
import com.larryTheCoder.cache.settings.IslandSettings;
4445
import com.larryTheCoder.locales.LocaleInstance;
@@ -225,6 +226,8 @@ public void onPlayerRespondForm(PlayerFormRespondedEvent event) {
225226
}
226227

227228
pd.saveIslandData();
229+
230+
p.sendMessage(plugin.getPrefix() + TextFormat.GREEN + "Successfully updated your island settings.");
228231
break;
229232
case FIRST_TIME_DELETE:
230233
// Check if the player closed this form
@@ -293,6 +296,8 @@ public void onPlayerRespondForm(PlayerFormRespondedEvent event) {
293296
}
294297

295298
pd3.saveIslandData();
299+
300+
p.sendMessage(plugin.getPrefix() + TextFormat.GREEN + "Successfully updated your island protection settings.");
296301
break;
297302
}
298303
}

src/main/java/com/larryTheCoder/island/IslandManager.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,9 @@ public int generateIslandKey(Location loc) {
281281
*/
282282
public int generateIslandKey(int x, int z, String level) {
283283
WorldSettings settings = plugin.getSettings(level);
284+
if (settings == null)
285+
throw new RuntimeException("A player is attempting to generate a non-island issued world named " + level);
286+
284287
return (x / settings.getIslandDistance() + z / settings.getIslandDistance() * Integer.MAX_VALUE) + settings.getLevelId();
285288
}
286289

@@ -374,11 +377,13 @@ public boolean locationIsOnIsland(Player player, Vector3 loc) {
374377
Location local = new Location(loc.x, loc.y, loc.z, player.getLevel());
375378
// Get the player's island from the grid if it exists
376379
IslandData island = plugin.getFastCache().getIslandData(local);
377-
CoopData pd = plugin.getFastCache().getRelations(local);
378-
379-
// On an island in the grid
380-
// In a protected zone but is on the list of acceptable players
381-
// Otherwise return false
382-
return island != null && (island.onIsland(local) && ((pd == null || pd.getMembers().contains(player.getName())) || island.getPlotOwner().equalsIgnoreCase(player.getName())));
380+
CoopData co = plugin.getFastCache().getRelations(local);
381+
382+
// On an island grid, if its the owner and is inside a list of acceptable players, return true,
383+
// otherwise false.
384+
return island != null &&
385+
island.onIsland(local) &&
386+
(island.getPlotOwner().equalsIgnoreCase(player.getName()) ||
387+
(co != null && co.getMembers().contains(player.getName())));
383388
}
384389
}

src/main/java/com/larryTheCoder/listener/Action.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ protected boolean notInWorld(@NonNull Location loc) {
7272
*/
7373
protected boolean actionAllowed(Location location, SettingsFlag flag) {
7474
IslandData island = plugin.getGrid().getProtectedIslandAt(location);
75-
if (island != null && island.getIgsSettings().getIgsFlag(flag)) {
76-
log.debug("DEBUG: Action is allowed by settings");
77-
return true;
75+
if (island != null) {
76+
log.debug("DEBUG: Action is determined by settings");
77+
return island.getIgsSettings().getIgsFlag(flag);
7878
}
7979
log.debug("DEBUG: Action is defined by settings");
8080
return Settings.defaultWorldSettings.get(flag);
@@ -88,9 +88,7 @@ protected boolean actionAllowed(Location location, SettingsFlag flag) {
8888
* @return true if allowed
8989
*/
9090
protected boolean actionAllowed(Player player, Location location, SettingsFlag flag) {
91-
if (player == null) {
92-
return actionAllowed(location, flag);
93-
}
91+
if (player == null) return actionAllowed(location, flag);
9492

9593
// This permission bypasses protection
9694
if (player.isOp() || hasPermission(player, "is.mod.bypassprotect")) {
@@ -103,15 +101,9 @@ protected boolean actionAllowed(Player player, Location location, SettingsFlag f
103101
return true;
104102
}
105103

106-
if (island == null || island.getPlotOwner() == null) {
107-
return false;
108-
}
109-
110-
if (island.getPlotOwner().equalsIgnoreCase(player.getName())) {
111-
return true;
112-
}
104+
if (island == null || island.getPlotOwner() == null) return false;
105+
if (island.getPlotOwner().equalsIgnoreCase(player.getName())) return true;
113106

114-
// Fixed
115107
return Settings.defaultWorldSettings.get(flag);
116108
}
117109

src/main/java/com/larryTheCoder/listener/IslandListener.java

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@
2929
import cn.nukkit.Player;
3030
import cn.nukkit.block.Block;
3131
import cn.nukkit.block.BlockLava;
32+
import cn.nukkit.entity.Entity;
3233
import cn.nukkit.entity.item.EntityPrimedTNT;
34+
import cn.nukkit.entity.mob.EntityMob;
35+
import cn.nukkit.entity.passive.EntityAnimal;
3336
import cn.nukkit.event.EventHandler;
3437
import cn.nukkit.event.EventPriority;
3538
import cn.nukkit.event.Listener;
3639
import cn.nukkit.event.block.BlockBreakEvent;
3740
import cn.nukkit.event.block.BlockPlaceEvent;
3841
import cn.nukkit.event.entity.EntityDamageByEntityEvent;
42+
import cn.nukkit.event.entity.EntityDamageEvent;
3943
import cn.nukkit.event.entity.EntityExplodeEvent;
4044
import cn.nukkit.event.inventory.CraftItemEvent;
45+
import cn.nukkit.event.inventory.InventoryPickupItemEvent;
4146
import cn.nukkit.event.player.*;
4247
import cn.nukkit.utils.TextFormat;
4348
import com.larryTheCoder.ASkyBlock;
@@ -139,26 +144,6 @@ public void onPlayerMove(PlayerMoveEvent e) {
139144
}
140145
}
141146

142-
@EventHandler(priority = EventPriority.LOWEST)
143-
public void onBucketUseEvent(PlayerBucketFillEvent e) {
144-
if (onBucketEvent(e.getPlayer(), e.getBlockClicked())) e.setCancelled();
145-
}
146-
147-
@EventHandler(priority = EventPriority.LOWEST)
148-
public void onBucketEmpty(PlayerBucketEmptyEvent e) {
149-
if (onBucketEvent(e.getPlayer(), e.getBlockClicked())) e.setCancelled();
150-
}
151-
152-
private boolean onBucketEvent(Player player, Block blockClicked) {
153-
if (notInWorld(player)) return false;
154-
if (actionAllowed(player, blockClicked.getLocation(), SettingsFlag.BUCKET)) {
155-
if (blockClicked instanceof BlockLava && actionAllowed(player, blockClicked.getLocation(), SettingsFlag.COLLECT_LAVA)) {
156-
return false;
157-
} else return !actionAllowed(player, blockClicked.getLocation(), SettingsFlag.COLLECT_WATER);
158-
}
159-
return true;
160-
}
161-
162147
@EventHandler(priority = EventPriority.LOW)
163148
public void onPlayerExecuteCommand(PlayerCommandPreprocessEvent event) {
164149
log.debug("DEBUG: " + event.getEventName());
@@ -173,10 +158,35 @@ public void onPlayerExecuteCommand(PlayerCommandPreprocessEvent event) {
173158
}
174159
}
175160

161+
@EventHandler(priority = EventPriority.LOW)
162+
public void onPlayerHitEvent(EntityDamageEvent e) {
163+
Entity target = e.getEntity();
164+
165+
if (notInWorld(target)) return;
166+
if (e instanceof EntityDamageByEntityEvent) {
167+
EntityDamageByEntityEvent damage = (EntityDamageByEntityEvent) e;
168+
Entity cause = damage.getDamager();
169+
170+
// Identifier for player mobs attack.
171+
if (!(cause instanceof Player)) {
172+
if (cause instanceof EntityAnimal) {
173+
if (actionAllowed(target.getLocation(), SettingsFlag.HURT_MOBS)) return;
174+
} else if (cause instanceof EntityMob) {
175+
if (actionAllowed(target.getLocation(), SettingsFlag.HURT_MONSTERS)) return;
176+
}
177+
} else {
178+
if (actionAllowed(target.getLocation(), SettingsFlag.PVP)) return;
179+
}
180+
181+
e.setCancelled();
182+
}
183+
}
184+
176185
@EventHandler(priority = EventPriority.LOWEST)
177186
public void onPlayerDropItem(PlayerDropItemEvent e) {
178187
log.debug("DEBUG: " + e.getEventName());
179188
log.debug("DEBUG: Item is " + e.getItem().toString());
189+
180190
Player p = e.getPlayer();
181191
if (notInWorld(p)) {
182192
log.debug("Event is not in world");
@@ -196,7 +206,37 @@ public void onPlayerDropItem(PlayerDropItemEvent e) {
196206
return;
197207
}
198208

199-
// Do not send any message, it may spam
209+
log.debug("Action not allowed: Drop item");
210+
e.setCancelled();
211+
}
212+
213+
@EventHandler(priority = EventPriority.LOWEST)
214+
public void onPlayerPickupItem(InventoryPickupItemEvent e) {
215+
log.debug("DEBUG: " + e.getEventName());
216+
log.debug("DEBUG: Item is " + e.getItem().toString());
217+
218+
Player p = (Player) e.getInventory().getHolder();
219+
if (notInWorld(p)) {
220+
log.debug("Event is not in world");
221+
return;
222+
}
223+
if (p.isOp() || hasPermission(p, "is.mod.bypassprotect")) {
224+
return;
225+
}
226+
227+
// Too bad that the item is not a vector3
228+
if (plugin.getIslandManager().locationIsOnIsland(p, e.getItem())) {
229+
log.debug("Action is allowed: Player on island");
230+
// You can do anything on your island
231+
return;
232+
}
233+
234+
if (actionAllowed(e.getItem().getLocation(), SettingsFlag.VISITOR_ITEM_PICKUP)) {
235+
log.debug("Action is allowed: Item pickup is allowed");
236+
return;
237+
}
238+
239+
log.debug("Action not allowed: Pickup item");
200240
e.setCancelled();
201241
}
202242

@@ -231,27 +271,27 @@ public void onPlayerInteract(PlayerInteractEvent e) {
231271
// No need stupid checks. Just cancel them
232272
p.sendMessage(getPrefix() + plugin.getLocale(e.getPlayer()).islandProtected);
233273
e.setCancelled();
234-
log.debug("Action is blocked");
274+
log.debug("Action is blocked PHYSICAL");
235275
return;
236276
}
237277
break;
238278
case LEFT_CLICK_BLOCK:
239279
// Player is interacting with an item. Check if it allowed
240-
if (!Utils.isItemAllowed(p, e.getItem())) {
280+
if (!Utils.isItemAllowed(p, e.getItem(), e.getBlock())) {
241281
// No need stupid checks. Just cancel them
242282
p.sendMessage(getPrefix() + plugin.getLocale(e.getPlayer()).islandProtected);
243283
e.setCancelled();
244-
log.debug("Action is blocked");
284+
log.debug("Action is blocked LEFT_CLICK_BLOCK");
245285
return;
246286
}
247287
break;
248288
case RIGHT_CLICK_BLOCK:
249289
// Player is clicking on something. Check if it allowed
250-
if (!Utils.isInventoryAllowed(p, e.getBlock())) {
290+
if (!Utils.isInventoryAllowed(p, e.getItem(), e.getBlock())) {
251291
// No need stupid checks. Just cancel them
252292
p.sendMessage(getPrefix() + plugin.getLocale(e.getPlayer()).islandProtected);
253293
e.setCancelled();
254-
log.debug("Action is blocked");
294+
log.debug("Action is blocked RIGHT_CLICK_BLOCK");
255295
return;
256296
}
257297
break;

src/main/java/com/larryTheCoder/utils/SettingsFlag.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
package com.larryTheCoder.utils;
2828

29+
import lombok.Getter;
30+
2931
/**
3032
* @author tastybento
3133
*/
@@ -83,12 +85,12 @@ public enum SettingsFlag {
8385
* Can collect lava
8486
* [Added]
8587
*/
86-
COLLECT_LAVA("Collect lava", 11),
88+
COLLECT_LAVA("Collect/Fill lava", 11),
8789
/**
8890
* Can collect water
8991
* [Added]
9092
*/
91-
COLLECT_WATER("Collect water", 12),
93+
COLLECT_WATER("Collect/Fill water", 12),
9294
/**
9395
* Can open chests or hoppers or dispensers
9496
* [Added]
@@ -157,11 +159,13 @@ public enum SettingsFlag {
157159
*/
158160
GATE("Use gates", 26),
159161
/**
160-
* Can hurt friendly mobs, e.g. cows
162+
* Can hurt friendly mobs, e.g. cows.
163+
* [Added]
161164
*/
162165
HURT_MOBS("Hurt mobs", 27),
163166
/**
164-
* Can hurt monsters
167+
* Can hurt monsters.
168+
* [Added]
165169
*/
166170
HURT_MONSTERS("Hurt monsters", 28),
167171
/**
@@ -192,7 +196,8 @@ public enum SettingsFlag {
192196
*/
193197
PRESSURE_PLATE("Activate pressure plates", 34),
194198
/**
195-
* Can do PVP in the overworld
199+
* Allows PvP inside the island compound
200+
* [Added]
196201
*/
197202
PVP("Allow PVP", 35),
198203
/**
@@ -214,19 +219,16 @@ public enum SettingsFlag {
214219
*/
215220
VISITOR_ITEM_DROP("Visitor drop item", 39),
216221
/**
217-
* Visitors can pick up items
222+
* Visitors can pick up items.
223+
* [Added]
218224
*/
219225
VISITOR_ITEM_PICKUP("Visitor pickup item", 40);
220226

221-
private int id;
222-
private String name;
227+
@Getter
228+
private final int id;
229+
@Getter
230+
private final String name;
223231

224-
/**
225-
* Create a value for the enum
226-
*
227-
* @param name The name for the enum
228-
* @param id The id for the enum
229-
*/
230232
SettingsFlag(String name, int id) {
231233
this.name = name;
232234
this.id = id;
@@ -244,12 +246,4 @@ public static SettingsFlag getFlag(int name) {
244246
public static SettingsFlag getFlag(String name) {
245247
return getFlag(Integer.parseInt(name));
246248
}
247-
248-
public int getId() {
249-
return this.id;
250-
}
251-
252-
public String getName() {
253-
return name;
254-
}
255249
}

0 commit comments

Comments
 (0)