Skip to content

Commit 3984c5c

Browse files
authored
Use more generic Entity for projectile type (#51)
* Use entity * Allow entity handling * Make proper init for DEFAULT_PROJECTILE_DEFINITION_REGISTRY
1 parent a5b5662 commit 3984c5c

7 files changed

Lines changed: 56 additions & 62 deletions

File tree

api-internal/src/main/java/at/pavlov/internal/key/registries/Registries.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,6 @@ public class Registries {
7272
.build()
7373
);
7474

75-
Key.from(
76-
List.of(
77-
"minecraft:snowball",
78-
"minecraft:egg",
79-
"minecraft:ender_pearl",
80-
"minecraft:experience_bottle",
81-
"minecraft:potion",
82-
"minecraft:lingering_potion",
83-
"minecraft:llama_spit"
84-
)
85-
).forEach( entry -> toAdd.add(
86-
new KeyedDefaultProjectile(entry)
87-
));
88-
8975
return toAdd;
9076
});
9177
SharedRegistryKeyValidator<ProjectilePhysics> validator = new SharedRegistryKeyValidator<>(CUSTOM_PROJECTILE_DEFINITION, DEFAULT_PROJECTILE_DEFINITION_REGISTRY);

cannons-bukkit/src/main/java/at/pavlov/cannons/Cannons.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@
3636
import at.pavlov.internal.CLogger;
3737
import at.pavlov.internal.Hook;
3838
import at.pavlov.internal.HookManager;
39+
import at.pavlov.internal.Key;
3940
import at.pavlov.internal.ModrinthUpdateChecker;
41+
import at.pavlov.internal.key.registries.Registries;
42+
import at.pavlov.internal.projectile.definition.KeyedDefaultProjectile;
4043
import lombok.Getter;
4144
import org.bstats.bukkit.Metrics;
4245
import org.bstats.charts.AdvancedPie;
4346
import org.bukkit.Bukkit;
4447
import org.bukkit.ChatColor;
4548
import org.bukkit.Location;
49+
import org.bukkit.NamespacedKey;
50+
import org.bukkit.entity.EntityType;
4651
import org.bukkit.entity.Player;
4752
import org.bukkit.inventory.ItemStack;
4853
import org.bukkit.plugin.PluginDescriptionFile;
@@ -95,6 +100,20 @@ public static Logger logger() {
95100
}
96101

97102
public void onLoad() {
103+
Registries.DEFAULT_PROJECTILE_DEFINITION_REGISTRY.setFrozen(false);
104+
for (EntityType type : EntityType.values()) {
105+
if (!type.isSpawnable()) continue;
106+
NamespacedKey key = type.getKey();
107+
Key cannonKey = Key.from(key.toString());
108+
109+
if (!Registries.DEFAULT_PROJECTILE_DEFINITION_REGISTRY.has(cannonKey)) {
110+
Registries.DEFAULT_PROJECTILE_DEFINITION_REGISTRY.register(
111+
new KeyedDefaultProjectile(cannonKey)
112+
);
113+
}
114+
}
115+
Registries.DEFAULT_PROJECTILE_DEFINITION_REGISTRY.setFrozen(true);
116+
98117
CLogger.logger = this.getLogger();
99118
// must be done in onLoad because "movecraft"
100119
AsyncTaskManager.initialize(this);

cannons-bukkit/src/main/java/at/pavlov/cannons/CreateExplosion.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private boolean breakBlock(Block block, List<Block> blocklist, Boolean superBrea
161161
* @param cannonball
162162
* @return the location after the piercing event
163163
*/
164-
private Location blockBreaker(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity) {
164+
private Location blockBreaker(FlyingProjectile cannonball, Entity projectile_entity) {
165165
Projectile projectile = cannonball.getProjectile();
166166

167167

@@ -757,7 +757,7 @@ private void addAffectedEntity(Entity entity) {
757757
* @param cannonball cannonball which hit the entity
758758
* @param entity entity hit
759759
*/
760-
public void directHit(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity, Entity entity) {
760+
public void directHit(FlyingProjectile cannonball, Entity projectile_entity, Entity entity) {
761761
// add damage to map - it will be applied later to the player
762762
double directHit = this.getDirectHitDamage(cannonball, entity);
763763
this.damageMap.put(entity, directHit);
@@ -772,7 +772,7 @@ public void directHit(FlyingProjectile cannonball, org.bukkit.entity.Projectile
772772
*
773773
* @param cannonball cannonball which will explode
774774
*/
775-
public void detonate(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity) {
775+
public void detonate(FlyingProjectile cannonball, Entity projectile_entity) {
776776
this.plugin.logDebug("detonate cannonball");
777777

778778
Projectile projectile = cannonball.getProjectile().clone();
@@ -994,7 +994,7 @@ else if (projectile.hasProperty(ProjectileProperties.OBSERVER)) {
994994
*
995995
* @param cannonball the flying projectile
996996
*/
997-
private void damageEntity(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity) {
997+
private void damageEntity(FlyingProjectile cannonball, Entity projectile_entity) {
998998
Projectile projectile = cannonball.getProjectile();
999999
Location impactLoc = cannonball.getImpactLocation();
10001000

@@ -1159,7 +1159,7 @@ private void spawnProjectiles(FlyingProjectile cannonball) {
11591159
*
11601160
* @param cannonball the flying projectile
11611161
*/
1162-
private void spawnFireworks(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity) {
1162+
private void spawnFireworks(FlyingProjectile cannonball, Entity projectile_entity) {
11631163
World world = cannonball.getWorld();
11641164
Projectile projectile = cannonball.getProjectile();
11651165

cannons-bukkit/src/main/java/at/pavlov/cannons/dao/wrappers/FireTaskWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void fireProjectiles(Projectile projectile, Player onlinePlayer) {
139139

140140
Vector vect = cannon.getFiringVector(true, true);
141141

142-
org.bukkit.entity.Projectile projectileEntity = ProjectileManager.getInstance().spawnProjectile(projectile, shooter, source, playerLoc, firingLoc, vect, cannon.getUID(), projectileCause);
142+
Entity projectileEntity = ProjectileManager.getInstance().spawnProjectile(projectile, shooter, source, playerLoc, firingLoc, vect, cannon.getUID(), projectileCause);
143143

144144
if (i == 0 && projectile.hasProperty(ProjectileProperties.SHOOTER_AS_PASSENGER) && onlinePlayer != null)
145145
projectileEntity.setPassenger(onlinePlayer);

cannons-bukkit/src/main/java/at/pavlov/cannons/projectile/FlyingProjectile.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class FlyingProjectile
4040
private final MovingObject predictor;
4141

4242

43-
public FlyingProjectile(Projectile projectile, org.bukkit.entity.Projectile projectile_entity, UUID shooterUID, org.bukkit.projectiles.ProjectileSource source, Location playerLoc, UUID cannonId, ProjectileCause projectileCause)
43+
public FlyingProjectile(Projectile projectile, org.bukkit.entity.Entity projectile_entity, UUID shooterUID, org.bukkit.projectiles.ProjectileSource source, Location playerLoc, UUID cannonId, ProjectileCause projectileCause)
4444
{
4545
//Validate.notNull(shooterUID, "shooterUID for the projectile can't be null");
4646
this.entityUID = projectile_entity.getUniqueId();
@@ -52,8 +52,8 @@ public FlyingProjectile(Projectile projectile, org.bukkit.entity.Projectile proj
5252
this.shooterUID = shooterUID;
5353
this.playerlocation = playerLoc;
5454
this.source = source;
55-
if (source != null)
56-
projectile_entity.setShooter(source);
55+
if (source != null && projectile_entity instanceof org.bukkit.entity.Projectile pe)
56+
pe.setShooter(source);
5757
this.projectileCause = projectileCause;
5858

5959
this.spawnTime = System.currentTimeMillis();
@@ -78,12 +78,12 @@ public UUID getShooterUID()
7878
*
7979
* @return
8080
*/
81-
public org.bukkit.entity.Projectile getProjectileEntity() {
81+
public Entity getProjectileEntity() {
8282
Entity e = Bukkit.getEntity(entityUID);
8383
if (e == null)
8484
return null;
8585

86-
return (org.bukkit.entity.Projectile) Bukkit.getEntity(entityUID);
86+
return Bukkit.getEntity(entityUID);
8787
}
8888

8989
public Projectile getProjectile()
@@ -104,7 +104,7 @@ public Location getPlayerlocation() {
104104
* check if the projectile in in a liquid
105105
* @return true if the projectile is in a liquid
106106
*/
107-
private boolean isInWaterCheck(org.bukkit.entity.Projectile projectile_entity)
107+
private boolean isInWaterCheck(Entity projectile_entity)
108108
{
109109
if(projectile_entity!=null)
110110
{
@@ -122,7 +122,7 @@ public boolean isInWater() {
122122
* returns if the projectile has entered the water surface and updates also inWater
123123
* @return true if the projectile has entered water
124124
*/
125-
public boolean updateWaterSurfaceCheck(org.bukkit.entity.Projectile projectile_entity)
125+
public boolean updateWaterSurfaceCheck(Entity projectile_entity)
126126
{
127127
inWater = isInWaterCheck(projectile_entity);
128128
boolean isSurface = !wasInWater && inWater;
@@ -153,7 +153,7 @@ public boolean isValid()
153153
* a projectile is valid if it has an entity, is not below -64 and younger than 1h (60*60*1000)
154154
* @return returns false if the projectile entity is null
155155
*/
156-
public boolean isValid(org.bukkit.entity.Projectile projectile_entity)
156+
public boolean isValid(Entity projectile_entity)
157157
{
158158
return (projectile_entity != null && projectile_entity.getLocation().getBlockY() > -64 && System.currentTimeMillis() < getSpawnTime() + 3600000);
159159
}
@@ -196,15 +196,15 @@ public Location getActualLocation(org.bukkit.entity.Projectile projectile_entity
196196
* returns the distance of the projectile location to the calculated location
197197
* @return distance of the projectile location to the calculated location
198198
*/
199-
public double distanceToProjectile(org.bukkit.entity.Projectile projectile_entity)
199+
public double distanceToProjectile(Entity projectile_entity)
200200
{
201201
return projectile_entity.getLocation().toVector().distance(predictor.getLoc());
202202
}
203203

204204
/**
205205
* teleports the projectile to the predicted location
206206
*/
207-
public void teleportToPrediction(org.bukkit.entity.Projectile projectile_entity)
207+
public void teleportToPrediction(Entity projectile_entity)
208208
{
209209
if (projectile_entity == null)
210210
return;

cannons-bukkit/src/main/java/at/pavlov/cannons/projectile/ProjectileManager.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private ProjectileManager(Cannons plugin) {
5151
this.plugin = plugin;
5252
}
5353

54-
public org.bukkit.entity.Projectile spawnProjectile(Projectile projectile, @NotNull UUID shooter, org.bukkit.projectiles.ProjectileSource source, Location playerLoc, Location spawnLoc, Vector velocity, UUID cannonId, ProjectileCause projectileCause) {
54+
public Entity spawnProjectile(Projectile projectile, @NotNull UUID shooter, org.bukkit.projectiles.ProjectileSource source, Location playerLoc, Location spawnLoc, Vector velocity, UUID cannonId, ProjectileCause projectileCause) {
5555
Preconditions.checkNotNull(shooter, "shooter for the projectile can't be null");
5656
World world = spawnLoc.getWorld();
5757

@@ -60,18 +60,15 @@ public org.bukkit.entity.Projectile spawnProjectile(Projectile projectile, @NotN
6060
spawnLoc.setPitch((float) (Math.acos(velocity.getY()/v)*180.0/Math.PI - 90));
6161
spawnLoc.setYaw((float) (Math.atan2(velocity.getZ(),velocity.getX())*180.0/Math.PI - 90));
6262

63-
org.bukkit.entity.Projectile projectileEntity = spawnProjectile(projectile, spawnLoc, velocity, world);
63+
Entity projectileEntity = spawnProjectile(projectile, spawnLoc, velocity, world);
6464

6565
if (projectile.isProjectileOnFire())
6666
projectileEntity.setFireTicks(100);
6767
//projectileEntity.setTicksLived(2);
6868

69-
70-
7169
//create a new flying projectile container
7270
FlyingProjectile cannonball = new FlyingProjectile(projectile, projectileEntity, shooter, source, playerLoc, cannonId, projectileCause);
7371

74-
7572
flyingProjectilesMap.put(cannonball.getUID(), cannonball);
7673

7774
//detonate timefused projectiles
@@ -80,43 +77,34 @@ public org.bukkit.entity.Projectile spawnProjectile(Projectile projectile, @NotN
8077
return projectileEntity;
8178
}
8279

83-
private org.bukkit.entity.@NotNull Projectile spawnProjectile(Projectile projectile, Location spawnLoc, Vector velocity, World world) {
84-
Entity pEntity = world.spawnEntity(spawnLoc, projectile.getProjectileEntity());
80+
private @NotNull Entity spawnProjectile(Projectile projectile, Location spawnLoc, Vector velocity, World world) {
81+
Entity entity = world.spawnEntity(spawnLoc, projectile.getProjectileEntity());
8582

8683
//calculate firing vector
87-
pEntity.setVelocity(velocity);
88-
89-
org.bukkit.entity.Projectile projectileEntity;
90-
try {
91-
projectileEntity = (org.bukkit.entity.Projectile) pEntity;
92-
} catch(Exception e) {
93-
plugin.logDebug("Can't convert EntityType " + pEntity.getType() + " to projectile. Using additional Snowball");
94-
projectileEntity = (org.bukkit.entity.Projectile) world.spawnEntity(spawnLoc, EntityType.SNOWBALL);
95-
projectileEntity.setVelocity(velocity);
96-
}
84+
entity.setVelocity(velocity);
9785

9886
CustomProjectileDefinition definition = Registries.CUSTOM_PROJECTILE_DEFINITION.of(projectile.getProjectileDefinitionKey());
9987
if (definition == null) {
100-
return projectileEntity;
88+
return entity;
10189
}
10290

103-
projectileEntity.setVisualFire(definition.isOnFire());
104-
projectileEntity.setGlowing(definition.isGlowing());
91+
entity.setVisualFire(definition.isOnFire());
92+
entity.setGlowing(definition.isGlowing());
10593

10694
ProjectilePhysics defaultCase = Registries.DEFAULT_PROJECTILE_DEFINITION_REGISTRY.of(definition.getEntityKey());
10795
if (defaultCase == null) {
10896
defaultCase = ProjectilePhysics.DEFAULT;
10997
}
11098

11199
if (!defaultCase.matches(definition)) {
112-
projectileEntity.setGravity(false);
100+
entity.setGravity(false);
113101
}
114102

115-
if (projectileEntity instanceof WitherSkull witherSkull) {
103+
if (entity instanceof WitherSkull witherSkull) {
116104
witherSkull.setCharged(definition.isCharged());
117-
} else if (projectileEntity instanceof AbstractArrow arrow) {
105+
} else if (entity instanceof AbstractArrow arrow) {
118106
arrow.setCritical(definition.isCritical());
119-
} else if (projectileEntity instanceof ThrowableProjectile throwable) {
107+
} else if (entity instanceof ThrowableProjectile throwable) {
120108
Material material = Material.matchMaterial(definition.getMaterial().full());
121109
if (material == null) {
122110
plugin.logSevere("In custom projectile: " + definition.getKey().full() + " the material key is invalid.");
@@ -132,7 +120,7 @@ public org.bukkit.entity.Projectile spawnProjectile(Projectile projectile, @NotN
132120
throwable.setItem(stack);
133121
}
134122

135-
return projectileEntity;
123+
return entity;
136124
}
137125

138126

@@ -154,7 +142,7 @@ private void detonateTimefuse(final FlyingProjectile cannonball) {
154142
return;
155143
}
156144
//detonate timefuse
157-
org.bukkit.entity.Projectile projectile_entity = fproj.getProjectileEntity();
145+
var projectile_entity = fproj.getProjectileEntity();
158146
//the projectile might be null
159147
if (projectile_entity != null) {
160148
CreateExplosion.getInstance().detonate(cannonball, projectile_entity);
@@ -196,7 +184,7 @@ public void directHitProjectile(Entity cannonball, Entity target) {
196184
return;
197185
}
198186

199-
org.bukkit.entity.Projectile projectile_entity = fproj.getProjectileEntity();
187+
Entity projectile_entity = fproj.getProjectileEntity();
200188
if (!fproj.hasDetonated() && cannonball.isValid()) {
201189
fproj.setHasDetonated(true);
202190
CreateExplosion.getInstance().directHit(fproj, projectile_entity, target);

cannons-bukkit/src/main/java/at/pavlov/cannons/scheduler/ProjectileObserver.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.bukkit.Bukkit;
1717
import org.bukkit.Location;
1818
import org.bukkit.block.Block;
19+
import org.bukkit.entity.Entity;
1920
import org.bukkit.entity.Player;
2021
import org.bukkit.util.Vector;
2122

@@ -53,7 +54,7 @@ public void setupScheduler()
5354

5455
for(var entry : projectiles.entrySet()) {
5556
FlyingProjectile cannonball = entry.getValue();
56-
org.bukkit.entity.Projectile projectile_entity = cannonball.getProjectileEntity();
57+
Entity projectile_entity = cannonball.getProjectileEntity();
5758

5859
Executor executor = (task) -> {
5960
if (plugin.isFolia()) {
@@ -101,7 +102,7 @@ public void setupScheduler()
101102
* if cannonball enters water it will spawn a splash effect
102103
* @param cannonball the projectile to check
103104
*/
104-
private void checkWaterImpact(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity) {
105+
private void checkWaterImpact(FlyingProjectile cannonball, Entity projectile_entity) {
105106

106107
//the projectile has passed the water surface, make a splash
107108
if (!cannonball.updateWaterSurfaceCheck(projectile_entity)) {
@@ -151,7 +152,7 @@ public void sendSplashToPlayers(Location loc, ItemHolder liquid, SoundHolder sou
151152
* teleports the player to new position of the cannonball
152153
* @param cannonball the FlyingProjectile to check
153154
*/
154-
private void updateTeleporter(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity)
155+
private void updateTeleporter(FlyingProjectile cannonball, Entity projectile_entity)
155156
{
156157
//do nothing if the teleport was already performed
157158
if (cannonball.isTeleported())
@@ -197,7 +198,7 @@ private void updateTeleporter(FlyingProjectile cannonball, org.bukkit.entity.Pro
197198
* @param cannonball projectile to update
198199
* @return true if the projectile must be removed
199200
*/
200-
private boolean updateProjectileLocation(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity)
201+
private boolean updateProjectileLocation(FlyingProjectile cannonball, Entity projectile_entity)
201202
{
202203
if (!plugin.getMyConfig().isKeepAliveEnabled())
203204
return false;
@@ -231,7 +232,7 @@ private boolean updateProjectileLocation(FlyingProjectile cannonball, org.bukkit
231232
* @param cannonball the cannonball entity entry of cannons
232233
* @param projectile_entity the entity of the projectile
233234
*/
234-
private void updateSmokeTrail(FlyingProjectile cannonball, org.bukkit.entity.Projectile projectile_entity) {
235+
private void updateSmokeTrail(FlyingProjectile cannonball, Entity projectile_entity) {
235236
Projectile proj = cannonball.getProjectile();
236237
int maxDist = plugin.getMyConfig().getImitatedBlockMaximumDistance();
237238
double smokeDist = proj.getSmokeTrailDistance()*(0.5 + random.nextDouble());

0 commit comments

Comments
 (0)