reformatted project
This commit is contained in:
@@ -15,47 +15,44 @@ 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) //TODO Why are some of these listeners not working?
|
||||
|
||||
.addListener(EntityAttackEvent.class, this::onAttack);
|
||||
this.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.teleport(this.position.withPitch(180).withYaw(0)));
|
||||
if(addEntityToInstanceEvent.getInstance() instanceof Spawnable) {
|
||||
this.scheduleNextTick((unused) -> this.teleport(this.position.withPitch(180).withYaw(0)));
|
||||
}
|
||||
|
||||
addEntityToInstanceEvent.getInstance().eventNode()
|
||||
.addListener(PlayerMoveEvent.class, playerMoveEvent -> {
|
||||
//TODO might be expensive
|
||||
if(this.position.distance(playerMoveEvent.getNewPosition()) < 5)
|
||||
this.lookAt(playerMoveEvent.getPlayer());
|
||||
})
|
||||
.addListener(PlayerEntityInteractEvent.class, playerEntityInteractEvent -> {
|
||||
if(playerEntityInteractEvent.getTarget() == this) {
|
||||
onInteract(playerEntityInteractEvent);
|
||||
}
|
||||
})
|
||||
.addListener(EntityAttackEvent.class, entityAttackEvent -> {
|
||||
if(entityAttackEvent.getTarget() == this) {
|
||||
onAttack(entityAttackEvent);
|
||||
}
|
||||
});
|
||||
.addListener(PlayerMoveEvent.class, playerMoveEvent -> {
|
||||
if(this.position.distance(playerMoveEvent.getNewPosition()) < 5)
|
||||
this.lookAt(playerMoveEvent.getPlayer());
|
||||
})
|
||||
.addListener(PlayerEntityInteractEvent.class, playerEntityInteractEvent -> {
|
||||
if(playerEntityInteractEvent.getTarget() == this) {
|
||||
this.onInteract(playerEntityInteractEvent);
|
||||
}
|
||||
})
|
||||
.addListener(EntityAttackEvent.class, entityAttackEvent -> {
|
||||
if(entityAttackEvent.getTarget() == this) {
|
||||
this.onAttack(entityAttackEvent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when instance of entity is set
|
||||
* @param addEntityToInstanceEvent
|
||||
*/
|
||||
protected void onSpawn(@NotNull AddEntityToInstanceEvent addEntityToInstanceEvent) {
|
||||
|
||||
@@ -63,7 +60,6 @@ public class InteractableEntity extends EntityCreature {
|
||||
|
||||
/**
|
||||
* Called when instance of entity is unset
|
||||
* @param removeEntityFromInstanceEvent
|
||||
*/
|
||||
protected void onDespawn(@NotNull RemoveEntityFromInstanceEvent removeEntityFromInstanceEvent) {
|
||||
|
||||
@@ -71,7 +67,6 @@ public class InteractableEntity extends EntityCreature {
|
||||
|
||||
/**
|
||||
* Called when a Player interacts with the entity
|
||||
* @param playerEntityInteractEvent
|
||||
*/
|
||||
protected void onInteract(@NotNull PlayerEntityInteractEvent playerEntityInteractEvent) {
|
||||
|
||||
@@ -79,7 +74,6 @@ public class InteractableEntity extends EntityCreature {
|
||||
|
||||
/**
|
||||
* Called when a Player attacks the entity
|
||||
* @param entityAttackEvent
|
||||
*/
|
||||
protected void onAttack(@NotNull EntityAttackEvent entityAttackEvent) {
|
||||
|
||||
|
||||
@@ -15,24 +15,19 @@ 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);
|
||||
this.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) -> {
|
||||
this.setItemStack(slot, item);
|
||||
this.addInventoryCondition((player, clickedSlot, clickType, inventoryConditionResult) -> {
|
||||
if(clickedSlot == slot) {
|
||||
if(closeAfter) player.closeInventory();
|
||||
callback.accept(new ItemClick(player, this, clickedSlot, item, clickType));
|
||||
@@ -47,30 +42,25 @@ public class InteractableInventory extends Inventory {
|
||||
|
||||
/**
|
||||
* Set Item without handler
|
||||
* @param item
|
||||
* @param slot
|
||||
*/
|
||||
protected void setDummyItem(ItemStack item, int slot) {
|
||||
this.setClickableItem(
|
||||
item,
|
||||
slot,
|
||||
itemClick -> {}
|
||||
item,
|
||||
slot,
|
||||
itemClick -> {
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected void setDummyItem(Material material, int slot) {
|
||||
this.setDummyItem(
|
||||
ItemStack.builder(material).customName(Component.text("")).build(),
|
||||
slot
|
||||
ItemStack.builder(material).customName(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) {
|
||||
|
||||
|
||||
@@ -14,11 +14,6 @@ public class ItemClick {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
@@ -29,22 +24,22 @@ public class ItemClick {
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public Inventory getInventory() {
|
||||
return inventory;
|
||||
return this.inventory;
|
||||
}
|
||||
|
||||
public int getClickedSlot() {
|
||||
return clickedSlot;
|
||||
return this.clickedSlot;
|
||||
}
|
||||
|
||||
public ItemStack getItem() {
|
||||
return item;
|
||||
return this.item;
|
||||
}
|
||||
|
||||
public ClickType getClickType() {
|
||||
return clickType;
|
||||
return this.clickType;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user