Skip to content

Commit

Permalink
Merge pull request #43 from PXAV/development
Browse files Browse the repository at this point in the history
Add changes for 0.2.0 - NPC Update
  • Loading branch information
PXAV committed Feb 15, 2021
2 parents 22ce489 + 506d51f commit 76a9981
Show file tree
Hide file tree
Showing 36 changed files with 1,388 additions and 350 deletions.
30 changes: 30 additions & 0 deletions CHANGELOG/kelp-v0.2.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# v0.2.0
> Release date: 15.02.2021
**The NPC update**:
* Many changes and enhancements in NPC system:
* You can now hook into the NPC heartbeat (custom NPC tick system) by creating an `NpcActivity`.
* Methods like `followHeadRotation()` or `imitateSneaking()` are now replaced with activities (`LookToActivity`, `SneakingActivity`), which also allows you to change the player the NPC is imitating the sneaking behavior of, etc. This makes NPC much more customizable and extensible.
* NPC heartbeat is now using asynchronous Kelp scheduler library (`RepeatingScheduler`)
* Add basic walking mechanics to NPCs with `WalkToDirectionActivity` and `WalkToTargetActivity`
* NPCs can now sprint, which also has effects on how fast they can walk (calculated with `MovementSpeed`)
* You can now modify the title lines of an NPC during its lifetime and change the content as well as the amount of the individual lines.
* An NPC is now bound to a specific player. This player has to be assigned before you spawn it for the first time. So you cannot use the same NPC instance for multiple players anymore.
* You can now teleport NPCs freely (`teleport(Location)`) and move their head with `lookTo(Location)` without having to refresh the metadata.
* You don't have to refresh your NPC anymore when you want to make it sneak/burn/invisible anymore. Refresh is now done automatically.
* You can now hide/show NPC custom names (remove the empty title line above the NPC's head)
* You can now set a custom tab name for an NPC that varies from its custom name and toggle whether the tab list name should be visible or invisible for other players.
* With `AutoSpawnActivity` you can now fix that NPCs despawn when they are out of range and become invisible for a player.
* You can now equip an NPC with armor and change the item in hand during NPC lifetime.
* You can now play animations on NPCs (such as a damage animation, swing arm, etc.)
* NPCs can now sleep inside a bed or act as a corpse on the ground (still experimental and not 100% optimized)
* It is no longer distinguished between `spawnLocation` and `currentLocation` of an NPC. You can only modify its current `location`, which will then be used to spawn and move the npc later.
* Add static factory for `KelpNpc`
* Add some NPC-related custom events:
* `NpcSpawnEvent`
* `NpcDespawnEvent`
* `NpcToggleSneakEvent`
* `NpcInteractEvent` (you can also listen to NPC interactions with an inline interaction listener `KelpNpc#onInteract(NpcInteractEvent)`)
* Create static factory for `KelpItem`, all inventory types and all default Kelp widgets
* Add `rows(int)` method to `AnimatedInventory` to make inventory sizes more readable
* Fix NullPointerException when creating a `KelpItem` with type `AIR`
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ There are version implementations for the following version implementations avai
<dependency>
<groupId>com.github.pxav.kelp</groupId>
<artifactId>core</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
</dependency>
```

### Gradle
```shell script
compile group: 'com.github.pxav.kelp', name: 'core', version: '0.1.0'
compile group: 'com.github.pxav.kelp', name: 'core', version: '0.2.0'
```

### Other build tools
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.github.pxav.kelp</groupId>
<artifactId>parent</artifactId>
<version>0.1.1</version>
<version>0.2.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/de/pxav/kelp/core/KelpPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*
* @author pxav
*/
@Plugin(name = "Kelp", version = "0.1.1")
@Plugin(name = "Kelp", version = "0.2.0")
@Author("pxav")
@Description("A cross version spigot framework.")
@Singleton
Expand Down Expand Up @@ -126,7 +126,6 @@ public void onEnable() {
public void onDisable() {
injector.getInstance(KelpApplicationRepository.class).disableApplications();

injector.getInstance(KelpNpcRepository.class).stopScheduler();
injector.getInstance(SidebarRepository.class).stopAllClusters();
logger().log("[SIDEBAR] Removed all animation clusters.");
injector.getInstance(ParticleEffectRepository.class).stopAllTimers();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.pxav.kelp.core.event.kelpevent.npc;

import de.pxav.kelp.core.npc.KelpNpc;
import org.bukkit.event.HandlerList;

public class NpcDespawnEvent extends NpcEvent {

private static final HandlerList handlers = new HandlerList();
private boolean isRemove;

public NpcDespawnEvent(KelpNpc npc, boolean isRemove) {
super(npc);
this.isRemove = isRemove;
}

public boolean isRemove() {
return isRemove;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.pxav.kelp.core.event.kelpevent.npc;

import de.pxav.kelp.core.npc.KelpNpc;
import de.pxav.kelp.core.player.KelpPlayer;
import org.bukkit.event.Event;

/**
Expand All @@ -20,4 +21,8 @@ public final KelpNpc getNpc() {
return this.npc;
}

public final KelpPlayer getPlayer() {
return npc.getPlayer();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.pxav.kelp.core.event.kelpevent.npc;

public enum NpcInteractAction {

RIGHT_CLICK,
LEFT_CLICK

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.pxav.kelp.core.event.kelpevent.npc;

import de.pxav.kelp.core.npc.KelpNpc;
import org.bukkit.event.HandlerList;

public class NpcInteractEvent extends NpcEvent {

private static final HandlerList handlers = new HandlerList();
private NpcInteractAction action;

public NpcInteractEvent(KelpNpc npc, NpcInteractAction action) {
super(npc);
this.action = action;
}

public NpcInteractAction getAction() {
return action;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.pxav.kelp.core.event.kelpevent.npc;

import de.pxav.kelp.core.npc.KelpNpc;
import org.bukkit.event.HandlerList;

public class NpcSpawnEvent extends NpcEvent {

private static final HandlerList handlers = new HandlerList();
private boolean initialSpawn;

public NpcSpawnEvent(KelpNpc npc, boolean initialSpawn) {
super(npc);
this.initialSpawn = initialSpawn;
}

public boolean isInitialSpawn() {
return initialSpawn;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package de.pxav.kelp.core.event.kelpevent.npc;

import de.pxav.kelp.core.npc.KelpNpc;
import org.bukkit.event.HandlerList;

// called after sneak action
public class NpcToggleSneakEvent extends NpcEvent {

private static final HandlerList handlers = new HandlerList();
private boolean initialSpawn;

public NpcToggleSneakEvent(KelpNpc npc) {
super(npc);
}

public boolean hasSneakedBefore() {
return !npc.isSneaking();
}

public boolean isSneakingNow() {
return npc.isSneaking();
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}

}
20 changes: 10 additions & 10 deletions core/src/main/java/de/pxav/kelp/core/inventory/item/KelpItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import de.pxav.kelp.core.KelpPlugin;
import de.pxav.kelp.core.inventory.listener.ClickListener;
import de.pxav.kelp.core.inventory.listener.KelpListenerRepository;
import de.pxav.kelp.core.inventory.material.KelpMaterial;
Expand Down Expand Up @@ -37,6 +38,14 @@ public KelpItem(ItemVersionTemplate itemVersionTemplate,
this.listenerRepository = listenerRepository;
}

public static KelpItem create() {
return new KelpItem(
KelpPlugin.getInjector().getInstance(ItemVersionTemplate.class),
KelpPlugin.getInjector().getInstance(ItemTagVersionTemplate.class),
KelpPlugin.getInjector().getInstance(KelpListenerRepository.class)
);
}

// the material of the item. If none is set, stone will be used
private KelpMaterial material = KelpMaterial.STONE;

Expand All @@ -48,11 +57,8 @@ public KelpItem(ItemVersionTemplate itemVersionTemplate,
// have their own item sorting methods.
private int slot;

// the inventory where the item is stored in
private Inventory parent;

// the display name of the item
private String displayName = " ";
private String displayName;

// the item description - aka. the item lore.
// Those are some lines of text below the display name.
Expand Down Expand Up @@ -295,8 +301,6 @@ public ItemStack getItemStack() {
// make the item unbreakable if needed.
if (this.unbreakable) {
itemStack = itemVersionTemplate.makeUnbreakable(itemStack);
} else {
itemStack = itemVersionTemplate.makeBreakable(itemStack);
}

// add a flag to cancel interactions by default, if nothing else has been defined
Expand All @@ -317,10 +321,6 @@ public ItemStack getItemStack() {
return itemStack;
}

public Inventory getParent() {
return this.parent;
}

/**
* Gets the slot, where the item should be placed
* in the parent-inventory.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.pxav.kelp.core.inventory.type;

import com.google.common.collect.Lists;
import de.pxav.kelp.core.KelpPlugin;
import de.pxav.kelp.core.animation.StaticTextAnimation;
import de.pxav.kelp.core.animation.TextAnimation;
import de.pxav.kelp.core.animation.TextAnimationFactory;
import de.pxav.kelp.core.inventory.item.KelpItem;
Expand Down Expand Up @@ -38,24 +40,33 @@ public class AnimatedInventory extends KelpInventory {

private WindowPacketTemplate windowPacketTemplate;
private InventoryVersionTemplate inventoryVersionTemplate;
private TextAnimationFactory textAnimationFactory;

public AnimatedInventory(WindowPacketTemplate windowPacketTemplate,
InventoryVersionTemplate inventoryVersionTemplate,
TextAnimationFactory textAnimationFactory) {
InventoryVersionTemplate inventoryVersionTemplate) {
this.windowPacketTemplate = windowPacketTemplate;
this.inventoryVersionTemplate = inventoryVersionTemplate;
this.textAnimationFactory = textAnimationFactory;
this.simpleWidgets = Lists.newArrayList();
this.groupedWidgets = Lists.newArrayList();
this.size = 54;
}

public static AnimatedInventory create() {
return new AnimatedInventory(
KelpPlugin.getInjector().getInstance(WindowPacketTemplate.class),
KelpPlugin.getInjector().getInstance(InventoryVersionTemplate.class)
);
}

public AnimatedInventory size(int size) {
this.size = size;
return this;
}

public AnimatedInventory rows(int rows) {
this.size = rows * 9;
return this;
}

public AnimatedInventory title(TextAnimation textAnimation) {
this.title = textAnimation;
return this;
Expand All @@ -78,7 +89,7 @@ public void updateTitleOnly(Player player, int state) {
@Override
public Inventory render() {
if (this.title == null) {
this.title = textAnimationFactory.newStaticTextAnimation("§8Inventory");
this.title = StaticTextAnimation.create().text("§8Inventory");
}

Inventory inventory = inventoryVersionTemplate.createInventory(this.size, title.states().get(0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@ public class KelpInventoryFactory {

private WindowPacketTemplate windowPacketTemplate;
private InventoryVersionTemplate inventoryVersionTemplate;
private TextAnimationFactory textAnimationFactory;

@Inject
public KelpInventoryFactory(WindowPacketTemplate windowPacketTemplate,
InventoryVersionTemplate inventoryVersionTemplate,
TextAnimationFactory textAnimationFactory) {
InventoryVersionTemplate inventoryVersionTemplate) {
this.windowPacketTemplate = windowPacketTemplate;
this.inventoryVersionTemplate = inventoryVersionTemplate;
this.textAnimationFactory = textAnimationFactory;
}

public AnimatedInventory newAnimatedInventory() {
return new AnimatedInventory(windowPacketTemplate, inventoryVersionTemplate, textAnimationFactory);
return new AnimatedInventory(windowPacketTemplate, inventoryVersionTemplate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ public class ItemWidget implements SimpleWidget {
// caches listeners to be added to the item, when the item itself is still null.
private Set<ClickListener> listenerCache;

ItemWidget() {}
ItemWidget() {
this.listenerCache = Sets.newHashSet();
}

public static ItemWidget create() {
return new ItemWidget();
}

/**
* Sets the item to be put into the inventory.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.math.DoubleMath;
import de.pxav.kelp.core.KelpPlugin;
import de.pxav.kelp.core.inventory.KelpInventoryRepository;
import de.pxav.kelp.core.inventory.item.KelpItem;
import de.pxav.kelp.core.player.KelpPlayer;
Expand Down Expand Up @@ -46,6 +47,12 @@ public class Pagination implements GroupedWidget {
this.contentItems = Lists.newArrayList();
}

public static Pagination create() {
return new Pagination(
KelpPlugin.getInjector().getInstance(KelpInventoryRepository.class)
);
}

/**
* Sets the content slots of the pagination widget.
*
Expand Down
Loading

0 comments on commit 76a9981

Please sign in to comment.