-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from D3v1s0m/2.X
Enhancements: Sitting Entities, Creaking NPCs, and Updated PacketEvents
- Loading branch information
Showing
14 changed files
with
244 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
plugin/src/main/java/lol/pyr/znpcsplus/entity/ArmorStandVehicleProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package lol.pyr.znpcsplus.entity; | ||
|
||
import io.github.retrooper.packetevents.util.SpigotConversionUtil; | ||
import lol.pyr.znpcsplus.api.entity.EntityProperty; | ||
import lol.pyr.znpcsplus.api.entity.PropertyHolder; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Represents an armor stand vehicle entity. | ||
* <p> | ||
* This entity is used to make the NPC sit on an invisible armor stand. | ||
* </p> | ||
*/ | ||
public class ArmorStandVehicleProperties implements PropertyHolder { | ||
|
||
private final Map<EntityPropertyImpl<?>, Object> propertyMap = new HashMap<>(); | ||
|
||
public ArmorStandVehicleProperties(EntityPropertyRegistryImpl propertyRegistry) { | ||
_setProperty(propertyRegistry.getByName("small", Boolean.class), true); | ||
_setProperty(propertyRegistry.getByName("invisible", Boolean.class), true); | ||
_setProperty(propertyRegistry.getByName("base_plate", Boolean.class), false); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public <T> T getProperty(EntityProperty<T> key) { | ||
return hasProperty(key) ? (T) propertyMap.get((EntityPropertyImpl<?>) key) : key.getDefaultValue(); | ||
} | ||
|
||
@Override | ||
public boolean hasProperty(EntityProperty<?> key) { | ||
return propertyMap.containsKey((EntityPropertyImpl<?>) key); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private <T> void _setProperty(EntityProperty<T> key, T value) { | ||
Object val = value; | ||
if (val instanceof ItemStack) val = SpigotConversionUtil.fromBukkitItemStack((ItemStack) val); | ||
|
||
setProperty((EntityPropertyImpl<T>) key, (T) val); | ||
} | ||
|
||
@Override | ||
public <T> void setProperty(EntityProperty<T> key, T value) { | ||
throw new UnsupportedOperationException("Cannot set properties on armor stands"); | ||
} | ||
|
||
@Override | ||
public void setItemProperty(EntityProperty<?> key, ItemStack value) { | ||
throw new UnsupportedOperationException("Cannot set item properties on armor stands"); | ||
} | ||
|
||
@Override | ||
public ItemStack getItemProperty(EntityProperty<?> key) { | ||
throw new UnsupportedOperationException("Cannot get item properties on armor stands"); | ||
} | ||
|
||
public <T> void setProperty(EntityPropertyImpl<T> key, T value) { | ||
if (key == null) return; | ||
if (value == null || value.equals(key.getDefaultValue())) propertyMap.remove(key); | ||
else propertyMap.put(key, value); | ||
} | ||
|
||
public Set<EntityProperty<?>> getAllProperties() { | ||
return Collections.unmodifiableSet(propertyMap.keySet()); | ||
} | ||
|
||
@Override | ||
public Set<EntityProperty<?>> getAppliedProperties() { | ||
return Collections.unmodifiableSet(propertyMap.keySet()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
plugin/src/main/java/lol/pyr/znpcsplus/entity/properties/EntitySittingProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package lol.pyr.znpcsplus.entity.properties; | ||
|
||
import com.github.retrooper.packetevents.protocol.entity.data.EntityData; | ||
import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes; | ||
import lol.pyr.znpcsplus.entity.ArmorStandVehicleProperties; | ||
import lol.pyr.znpcsplus.entity.EntityPropertyImpl; | ||
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; | ||
import lol.pyr.znpcsplus.entity.PacketEntity; | ||
import lol.pyr.znpcsplus.packets.PacketFactory; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.Map; | ||
|
||
public class EntitySittingProperty extends EntityPropertyImpl<Boolean> { | ||
private final PacketFactory packetFactory; | ||
private final EntityPropertyRegistryImpl propertyRegistry; | ||
|
||
public EntitySittingProperty(PacketFactory packetFactory, EntityPropertyRegistryImpl propertyRegistry) { | ||
super("entity_sitting", false, Boolean.class); | ||
this.packetFactory = packetFactory; | ||
this.propertyRegistry = propertyRegistry; | ||
} | ||
|
||
@Override | ||
public void apply(Player player, PacketEntity entity, boolean isSpawned, Map<Integer, EntityData> properties) { | ||
boolean sitting = entity.getProperty(this); | ||
if (sitting) { | ||
if (entity.getVehicle() == null) { | ||
PacketEntity vehiclePacketEntity = new PacketEntity(packetFactory, new ArmorStandVehicleProperties(propertyRegistry), | ||
entity.getViewable(), EntityTypes.ARMOR_STAND, entity.getLocation().withY(entity.getLocation().getY() - 0.9)); | ||
entity.setVehicle(vehiclePacketEntity); | ||
} | ||
} else if (entity.getVehicle() != null) { | ||
entity.setVehicle(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.