Initial commit
This commit is contained in:
@ -0,0 +1,93 @@
|
||||
package eu.mhsl.minenet.minigames.shared.entity;
|
||||
|
||||
import eu.mhsl.minenet.minigames.instance.Spawnable;
|
||||
import net.minestom.server.entity.EntityCreature;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.entity.ai.target.ClosestEntityTarget;
|
||||
import net.minestom.server.event.entity.EntityAttackEvent;
|
||||
import net.minestom.server.event.instance.AddEntityToInstanceEvent;
|
||||
import net.minestom.server.event.instance.RemoveEntityFromInstanceEvent;
|
||||
import net.minestom.server.event.player.PlayerEntityInteractEvent;
|
||||
import net.minestom.server.event.player.PlayerMoveEvent;
|
||||
import net.minestom.server.timer.ExecutionType;
|
||||
import net.minestom.server.timer.TaskSchedule;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
|
||||
public class InteractableEntity extends EntityCreature {
|
||||
|
||||
/**
|
||||
* Declares an Entity with direct callbacks on interaction
|
||||
* @param entityType type of entity
|
||||
*/
|
||||
public InteractableEntity(@NotNull EntityType entityType) {
|
||||
super(entityType);
|
||||
|
||||
eventNode()
|
||||
.addListener(AddEntityToInstanceEvent.class, this::setInstanceEvent)
|
||||
|
||||
.addListener(AddEntityToInstanceEvent.class, this::onSpawn)
|
||||
.addListener(RemoveEntityFromInstanceEvent.class, this::onDespawn)
|
||||
.addListener(PlayerEntityInteractEvent.class, this::onInteract)
|
||||
.addListener(EntityAttackEvent.class, this::onAttack);
|
||||
}
|
||||
|
||||
private void setInstanceEvent(@NotNull AddEntityToInstanceEvent addEntityToInstanceEvent) {
|
||||
if(addEntityToInstanceEvent.getInstance() instanceof Spawnable instance) {
|
||||
scheduleNextTick((unused) -> {
|
||||
this.lookAt(instance.getSpawn()); //TODO only works someitmes?
|
||||
});
|
||||
}
|
||||
|
||||
addEntityToInstanceEvent.getInstance().eventNode()
|
||||
.addListener(PlayerEntityInteractEvent.class, playerEntityInteractEvent -> {
|
||||
if(playerEntityInteractEvent.getTarget() == this) onInteract(playerEntityInteractEvent);
|
||||
})
|
||||
.addListener(EntityAttackEvent.class, entityAttackEvent -> {
|
||||
if(entityAttackEvent.getTarget() == this) onAttack(entityAttackEvent);
|
||||
})
|
||||
.addListener(PlayerMoveEvent.class, playerMoveEvent -> {
|
||||
//TODO this is heavy in production
|
||||
//maybe store the player and update to the closest Entity only periodic
|
||||
scheduler().submitTask(() -> {
|
||||
setTarget(new ClosestEntityTarget(this, 5, entity -> entity instanceof Player).findTarget());
|
||||
if(getTarget() != null) lookAt(getTarget());
|
||||
|
||||
return TaskSchedule.stop();
|
||||
}, ExecutionType.ASYNC);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when instance of entity is set
|
||||
* @param addEntityToInstanceEvent
|
||||
*/
|
||||
protected void onSpawn(@NotNull AddEntityToInstanceEvent addEntityToInstanceEvent) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when instance of entity is unset
|
||||
* @param removeEntityFromInstanceEvent
|
||||
*/
|
||||
protected void onDespawn(@NotNull RemoveEntityFromInstanceEvent removeEntityFromInstanceEvent) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a Player interacts with the entity
|
||||
* @param playerEntityInteractEvent
|
||||
*/
|
||||
protected void onInteract(@NotNull PlayerEntityInteractEvent playerEntityInteractEvent) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a Player attacks the entity
|
||||
* @param entityAttackEvent
|
||||
*/
|
||||
protected void onAttack(@NotNull EntityAttackEvent entityAttackEvent) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package eu.mhsl.minenet.minigames.shared.inventory;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.inventory.Inventory;
|
||||
import net.minestom.server.inventory.InventoryType;
|
||||
import net.minestom.server.inventory.click.ClickType;
|
||||
import net.minestom.server.inventory.condition.InventoryConditionResult;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.item.Material;
|
||||
import net.minestom.server.timer.ExecutionType;
|
||||
import net.minestom.server.timer.TaskSchedule;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class InteractableInventory extends Inventory {
|
||||
/**
|
||||
* Defines an Inventory with direct callbacks for ItemSlots
|
||||
* @param inventoryType
|
||||
* @param title
|
||||
*/
|
||||
protected InteractableInventory(@NotNull InventoryType inventoryType, @NotNull Component title) {
|
||||
super(inventoryType, title);
|
||||
|
||||
addInventoryCondition(this::onClick);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Item with Callback
|
||||
* @param item
|
||||
* @param slot
|
||||
* @param callback
|
||||
*/
|
||||
protected void setClickableItem(ItemStack item, int slot, Consumer<ItemClick> callback, boolean closeAfter) {
|
||||
setItemStack(slot, item);
|
||||
addInventoryCondition((player, clickedSlot, clickType, inventoryConditionResult) -> {
|
||||
if(clickedSlot == slot) {
|
||||
if(closeAfter) player.closeInventory();
|
||||
callback.accept(new ItemClick(player, this, clickedSlot, item, clickType));
|
||||
}
|
||||
inventoryConditionResult.setCancel(true);
|
||||
});
|
||||
}
|
||||
|
||||
protected void setClickableItem(ItemStack item, int slot, Consumer<ItemClick> callback) {
|
||||
this.setClickableItem(item, slot, callback, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Item without handler
|
||||
* @param item
|
||||
* @param slot
|
||||
*/
|
||||
protected void setDummyItem(ItemStack item, int slot) {
|
||||
this.setClickableItem(
|
||||
item,
|
||||
slot,
|
||||
itemClick -> {}
|
||||
);
|
||||
}
|
||||
|
||||
protected void setDummyItem(Material material, int slot) {
|
||||
this.setDummyItem(
|
||||
ItemStack.builder(material).displayName(Component.text("")).build(),
|
||||
slot
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* You may want to Override this method to get more generic click events
|
||||
* @param player
|
||||
* @param slot
|
||||
* @param clickType
|
||||
* @param inventoryConditionResult
|
||||
*/
|
||||
protected void onClick(Player player, int slot, ClickType clickType, InventoryConditionResult inventoryConditionResult) {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package eu.mhsl.minenet.minigames.shared.inventory;
|
||||
|
||||
import net.minestom.server.entity.Player;
|
||||
import net.minestom.server.inventory.Inventory;
|
||||
import net.minestom.server.inventory.click.ClickType;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
|
||||
public class ItemClick {
|
||||
private final Player player;
|
||||
private final InteractableInventory inventory;
|
||||
private final int clickedSlot;
|
||||
private final ItemStack item;
|
||||
private final ClickType clickType;
|
||||
|
||||
/**
|
||||
* Describes a click on an Item from an IntractableInventory
|
||||
* @param player
|
||||
* @param inventory
|
||||
* @param clickedSlot
|
||||
* @param item
|
||||
* @param clickType
|
||||
*/
|
||||
public ItemClick(Player player, InteractableInventory inventory, int clickedSlot, ItemStack item, ClickType clickType) {
|
||||
this.player = player;
|
||||
this.inventory = inventory;
|
||||
this.clickedSlot = clickedSlot;
|
||||
this.item = item;
|
||||
this.clickType = clickType;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public Inventory getInventory() {
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public int getClickedSlot() {
|
||||
return clickedSlot;
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return item;
|
||||
}
|
||||
|
||||
public ClickType getClickType() {
|
||||
return clickType;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user