Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
cyilin committed Feb 11, 2019
1 parent b358886 commit 66cb523
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/main/java/cat/nyaa/HamsterEcoHelper/HamsterEcoHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public void onEnable() {
this.ess = (Essentials) getServer().getPluginManager().getPlugin("Essentials");
}
eventHandler = new Events(this);
kitManager=new KitManager(this);
kitListener=new KitListener(this);
kitManager = new KitManager(this);
kitListener = new KitListener(this);
}

@Override
Expand Down Expand Up @@ -106,6 +106,7 @@ public void reload() {
this.ess = (Essentials) getServer().getPluginManager().getPlugin("Essentials");
}
adsManager = new AdsManager(this);
kitManager = new KitManager(this);
}
}

Expand Down
43 changes: 40 additions & 3 deletions src/main/java/cat/nyaa/HamsterEcoHelper/kit/KitListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.block.*;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;

public class KitListener implements Listener {
Expand Down Expand Up @@ -82,6 +81,8 @@ public void onBreakBlock(BlockBreakEvent event) {
player.sendMessage(I18n.format("user.kit.sign.remove", kitSign.kitName));
}
}
} else if (plugin.kitManager.isAttachedBlock(block)) {
event.setCancelled(true);
}
}

Expand Down Expand Up @@ -140,5 +141,41 @@ public void onPlayerInteract(PlayerInteractEvent e) {
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntityExplode(EntityExplodeEvent event) {
event.blockList().removeIf(block -> plugin.kitManager.getKitSign(block) != null || plugin.kitManager.isAttachedBlock(block));
}


@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockExplode(BlockExplodeEvent event) {
event.blockList().removeIf(block -> plugin.kitManager.getKitSign(block) != null || plugin.kitManager.isAttachedBlock(block));
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockBurn(BlockBurnEvent event) {
if (plugin.kitManager.getKitSign(event.getBlock()) != null || plugin.signShopManager.isAttachedBlock(event.getBlock())) {
event.setCancelled(true);
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPistonExtend(BlockPistonExtendEvent event) {
for (Block block : event.getBlocks()) {
if (plugin.kitManager.isAttachedBlock(block)) {
event.setCancelled(true);
}
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onBlockPistonRetract(BlockPistonRetractEvent event) {
for (Block block : event.getBlocks()) {
if (plugin.kitManager.isAttachedBlock(block)) {
event.setCancelled(true);
}
}
}
}

23 changes: 23 additions & 0 deletions src/main/java/cat/nyaa/HamsterEcoHelper/kit/KitManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@
import cat.nyaa.HamsterEcoHelper.HamsterEcoHelper;
import cat.nyaa.HamsterEcoHelper.database.Kit;
import cat.nyaa.HamsterEcoHelper.database.KitSign;
import cat.nyaa.HamsterEcoHelper.signshop.SignShopManager;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class KitManager {
public Set<KitSign> kitSigns = new HashSet<>();
public HashMap<Location, Block> attachedBlocks = new HashMap<>();
private HamsterEcoHelper plugin;

public KitManager(HamsterEcoHelper pl) {
plugin = pl;
kitSigns.addAll(plugin.database.getAllKitSign());
updateAttachedBlocks();
}

public boolean createKit(String kitName, List<ItemStack> items, boolean removeOld) {
Expand Down Expand Up @@ -45,12 +50,14 @@ public void createKitSign(Block block, String kitName, int price, KitSign.SignTy
kitSign.type = type;
plugin.database.createKitSign(kitSign);
kitSigns.add(kitSign);
attachedBlocks.put(block.getLocation(), SignShopManager.getAttachedBlock(block));
}

public void removeKitSign(Block block) {
KitSign sign = getKitSign(block);
plugin.database.removeKitSign(sign);
kitSigns.remove(sign);
attachedBlocks.remove(block.getLocation());
}

public KitSign getKitSign(Block block) {
Expand All @@ -62,5 +69,21 @@ public KitSign getKitSign(Block block) {
}
return null;
}

public void updateAttachedBlocks() {
if (kitSigns.isEmpty()) {
return;
}
attachedBlocks = new HashMap<>();
for (KitSign sign : kitSigns) {
if (sign.getLocation() != null && SignShopManager.isSign(sign.getLocation().getBlock())) {
attachedBlocks.put(sign.getLocation().clone(), SignShopManager.getAttachedBlock(sign.getLocation().getBlock()));
}
}
}

public boolean isAttachedBlock(Block block) {
return attachedBlocks.containsValue(block);
}
}

5 changes: 3 additions & 2 deletions src/main/resources/lang/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,14 @@ user:
not_exist: kit %s does not exist
give:
success: "given %s to %s"
error: %s does not have enough inventory space
error: '%s does not have enough inventory space'
receive: You received %s
remove: kit % removed
remove: kit %s removed
not_enough_space: not enough inventory space
reset:
success: reset success
sign:
remove: sign removed
line_0:
BUY: '&1[&2kit&1]'
RESET: '&1[&2resetkit&1]'
Expand Down

0 comments on commit 66cb523

Please sign in to comment.