Add persistance to beehive population

This commit is contained in:
Tomás F 2020-07-03 15:23:40 +01:00
parent 3e74124af2
commit 3d7562061f
2 changed files with 50 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import com.tomff.beesplus.core.items.CustomItemManager;
import com.tomff.beesplus.core.migrations.AddField;
import com.tomff.beesplus.core.migrations.Migration;
import com.tomff.beesplus.core.migrations.MigrationsExecutor;
import com.tomff.beesplus.handlers.BeehiveHandler;
import com.tomff.beesplus.handlers.DamageHandler;
import com.tomff.beesplus.handlers.RightClickHandler;
import com.tomff.beesplus.items.*;
@ -36,6 +37,7 @@ public class BeesPlus extends JavaPlugin {
customItemManager = new CustomItemManager(this);
getServer().getPluginManager().registerEvents(new GuiHandler(this), this);
getServer().getPluginManager().registerEvents(new BeehiveHandler(), this);
getServer().getPluginManager().registerEvents(new RightClickHandler(this), this);
if (!loadLocale()) {

View File

@ -0,0 +1,48 @@
package com.tomff.beesplus.handlers;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDropItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
public class BeehiveHandler implements Listener {
@EventHandler
public void onBeeHiveBreak(BlockDropItemEvent event) {
BlockState blockState = event.getBlockState();
Material blockType = blockState.getType();
if (blockType != Material.BEEHIVE) {
return;
}
if (event.getItems().size() == 0) {
return;
}
event.getItems()
.stream()
.filter(this::validBeehive)
.findFirst()
.ifPresent(item -> saveState(blockState, item));
}
private boolean validBeehive(Item item) {
ItemStack itemStack = item.getItemStack();
return (!itemStack.hasItemMeta() && itemStack.getType() == Material.BEEHIVE);
}
private void saveState(BlockState state, Item item) {
ItemStack itemStack = item.getItemStack();
BlockStateMeta itemMeta = (BlockStateMeta) itemStack.getItemMeta();
itemMeta.setBlockState(state);
itemStack.setItemMeta(itemMeta);
item.setItemStack(itemStack);
}
}