Added checking for end platform overwrite

This commit is contained in:
J0nasL 2023-07-19 01:20:08 -04:00
parent 0d7db4a572
commit 4a08e6f133
2 changed files with 40 additions and 13 deletions

View file

@ -108,10 +108,9 @@ public class EggController {
/**
* Spawns a new egg item at the given location, sets it to invincible if enabled in the given config.
* This should trigger the item drop event, so it should not be necessary to immediately update the data file with the returned item.
* @return the egg item that was spawned
*/
public static Item spawnEggItem(Location loc, Configuration config, Data data) {
public static void spawnEggItem(Location loc, Configuration config, Data data) {
ItemStack egg = new ItemStack(Material.DRAGON_EGG);
egg.setAmount(1);
Item drop = loc.getWorld().dropItem(loc, egg);
@ -121,7 +120,7 @@ public class EggController {
if (config.getEggInvulnerable()) {
drop.setInvulnerable(true);
}
return drop;
data.updateEggLocation(drop);
}
/**

View file

@ -20,7 +20,10 @@ import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ItemDespawnEvent;
import org.bukkit.event.hanging.HangingBreakEvent;
import org.bukkit.event.hanging.HangingBreakEvent.RemoveCause;
import org.bukkit.event.server.PluginDisableEvent;
import org.bukkit.event.world.PortalCreateEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.event.world.PortalCreateEvent.CreateReason;
import org.bukkit.inventory.ItemStack;
import io.github.J0hnL0cke.egghunt.Model.Configuration;
@ -132,11 +135,34 @@ public class EggDestroyListener implements Listener {
@EventHandler(priority = EventPriority.HIGHEST)
public void onGrow(StructureGrowEvent event) {
if (event.getSpecies().equals(TreeType.RED_MUSHROOM)) {
logger.log("Red mushroom grown");
List<BlockState> blocks = event.getBlocks();
handleBlockStateReplace(blocks);
}
}
/**
* Prevent end platform regeneration from overwriting the egg
*/
@EventHandler(priority = EventPriority.MONITOR)
public void onPortalCreate(PortalCreateEvent event) {
logger.log("Portal created");
if (event.getReason().equals(CreateReason.END_PLATFORM)) {
ArrayList<Block> eggs = findEggs(getBlockListFromBlockStateList(event.getBlocks()));
//for some reason PortalCreateEvent's block list is not a shallow copy, unlike EntityExplode and StructureGrow Events
//that means editing the list does not change which blocks are actually affected by the event
//as a workaround, allow egg blocks to be destroyed, but respawn the egg as an item
for (Block egg : eggs) {
EggController.spawnEggItem(egg.getLocation(), config, data);
}
logger.log("Egg overwritten by the end spawn platform, spawning egg item at block location");
}
}
/**
* Listens for the dragon destroying the egg by flying into it
*/
@ -215,27 +241,29 @@ public class EggDestroyListener implements Listener {
}
}
private void handleBlockStateReplace(List<BlockState> blockStates) {
private ArrayList<Block> getBlockListFromBlockStateList(List<BlockState> blockStates) {
ArrayList<Block> blocks = new ArrayList<>();
for (BlockState blockState : blockStates) {
blocks.add(blockState.getBlock());
}
return blocks;
}
private void handleBlockStateReplace(List<BlockState> blockStates) {
ArrayList<Block> blocks = getBlockListFromBlockStateList(blockStates);
ArrayList<Block> eggs = findEggs(blocks);
if (!eggs.isEmpty()) {
if (config.getEggInvulnerable()) { //prevent egg destruction
for (Block b : eggs) {
for (BlockState state : blockStates) {
if (b.equals(state.getBlock())) {
blockStates.remove(state); //remove egg from list of blocks to delete
}
}
for (int i = eggs.size()-1; i >= 0; i--) { //iterate over every egg found backwards
int index = blocks.indexOf(eggs.get(i)); //get where the egg is in block list
blockStates.remove(index); //remove that item from the blockstate list
}
//log("Prevented egg from being replaced");
log("Prevented egg from being replaced");
} else { //if egg will be replaced, make sure it gets removed
for (Block b : eggs) {
Egg.removeEgg(b); //delete egg