aboutsummaryrefslogtreecommitdiffhomepage
path: root/patch-remap/mache-vineflower/net/minecraft/world/entity/vehicle/VehicleEntity.java.patch
blob: ffeffb251fbd7c712695b99a4ecd30863b2cf05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
--- a/net/minecraft/world/entity/vehicle/VehicleEntity.java
+++ b/net/minecraft/world/entity/vehicle/VehicleEntity.java
@@ -13,7 +13,14 @@
 import net.minecraft.world.level.Level;
 import net.minecraft.world.level.gameevent.GameEvent;
 
+// CraftBukkit start
+import org.bukkit.entity.Vehicle;
+import org.bukkit.event.vehicle.VehicleDamageEvent;
+import org.bukkit.event.vehicle.VehicleDestroyEvent;
+// CraftBukkit end
+
 public abstract class VehicleEntity extends Entity {
+
     protected static final EntityDataAccessor<Integer> DATA_ID_HURT = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.INT);
     protected static final EntityDataAccessor<Integer> DATA_ID_HURTDIR = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.INT);
     protected static final EntityDataAccessor<Float> DATA_ID_DAMAGE = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.FLOAT);
@@ -24,74 +31,108 @@
 
     @Override
     public boolean hurt(DamageSource source, float amount) {
-        if (this.level().isClientSide || this.isRemoved()) {
-            return true;
-        } else if (this.isInvulnerableTo(source)) {
-            return false;
-        } else {
-            this.setHurtDir(-this.getHurtDir());
-            this.setHurtTime(10);
-            this.markHurt();
-            this.setDamage(this.getDamage() + amount * 10.0F);
-            this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
-            boolean flag = source.getEntity() instanceof Player && ((Player)source.getEntity()).getAbilities().instabuild;
-            if ((flag || !(this.getDamage() > 40.0F)) && !this.shouldSourceDestroy(source)) {
-                if (flag) {
-                    this.discard();
-                }
+        if (!this.level().isClientSide && !this.isRemoved()) {
+            if (this.isInvulnerableTo(source)) {
+                return false;
             } else {
-                this.destroy(source);
-            }
+                // CraftBukkit start
+                Vehicle vehicle = (Vehicle) this.getBukkitEntity();
+                org.bukkit.entity.Entity attacker = (source.getEntity() == null) ? null : source.getEntity().getBukkitEntity();
 
+                VehicleDamageEvent event = new VehicleDamageEvent(vehicle, attacker, (double) amount);
+                this.level().getCraftServer().getPluginManager().callEvent(event);
+
+                if (event.isCancelled()) {
+                    return false;
+                }
+                amount = (float) event.getDamage();
+                // CraftBukkit end
+                this.setHurtDir(-this.getHurtDir());
+                this.setHurtTime(10);
+                this.markHurt();
+                this.setDamage(this.getDamage() + amount * 10.0F);
+                this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
+                boolean flag = source.getEntity() instanceof Player && ((Player) source.getEntity()).getAbilities().instabuild;
+
+                if ((flag || this.getDamage() <= 40.0F) && !this.shouldSourceDestroy(source)) {
+                    if (flag) {
+                        // CraftBukkit start
+                        VehicleDestroyEvent destroyEvent = new VehicleDestroyEvent(vehicle, attacker);
+                        this.level().getCraftServer().getPluginManager().callEvent(destroyEvent);
+
+                        if (destroyEvent.isCancelled()) {
+                            this.setDamage(40.0F); // Maximize damage so this doesn't get triggered again right away
+                            return true;
+                        }
+                        // CraftBukkit end
+                        this.discard();
+                    }
+                } else {
+                    // CraftBukkit start
+                    VehicleDestroyEvent destroyEvent = new VehicleDestroyEvent(vehicle, attacker);
+                    this.level().getCraftServer().getPluginManager().callEvent(destroyEvent);
+
+                    if (destroyEvent.isCancelled()) {
+                        this.setDamage(40.0F); // Maximize damage so this doesn't get triggered again right away
+                        return true;
+                    }
+                    // CraftBukkit end
+                    this.destroy(source);
+                }
+
+                return true;
+            }
+        } else {
             return true;
         }
     }
 
-    boolean shouldSourceDestroy(DamageSource damageSource) {
+    boolean shouldSourceDestroy(DamageSource damagesource) {
         return false;
     }
 
     public void destroy(Item item) {
         this.kill();
         if (this.level().getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
-            ItemStack itemStack = new ItemStack(item);
+            ItemStack itemstack = new ItemStack(item);
+
             if (this.hasCustomName()) {
-                itemStack.setHoverName(this.getCustomName());
+                itemstack.setHoverName(this.getCustomName());
             }
 
-            this.spawnAtLocation(itemStack);
+            this.spawnAtLocation(itemstack);
         }
     }
 
     @Override
     protected void defineSynchedData() {
-        this.entityData.define(DATA_ID_HURT, 0);
-        this.entityData.define(DATA_ID_HURTDIR, 1);
-        this.entityData.define(DATA_ID_DAMAGE, 0.0F);
+        this.entityData.define(VehicleEntity.DATA_ID_HURT, 0);
+        this.entityData.define(VehicleEntity.DATA_ID_HURTDIR, 1);
+        this.entityData.define(VehicleEntity.DATA_ID_DAMAGE, 0.0F);
     }
 
     public void setHurtTime(int i) {
-        this.entityData.set(DATA_ID_HURT, i);
+        this.entityData.set(VehicleEntity.DATA_ID_HURT, i);
     }
 
     public void setHurtDir(int i) {
-        this.entityData.set(DATA_ID_HURTDIR, i);
+        this.entityData.set(VehicleEntity.DATA_ID_HURTDIR, i);
     }
 
     public void setDamage(float f) {
-        this.entityData.set(DATA_ID_DAMAGE, f);
+        this.entityData.set(VehicleEntity.DATA_ID_DAMAGE, f);
     }
 
     public float getDamage() {
-        return this.entityData.get(DATA_ID_DAMAGE);
+        return (Float) this.entityData.get(VehicleEntity.DATA_ID_DAMAGE);
     }
 
     public int getHurtTime() {
-        return this.entityData.get(DATA_ID_HURT);
+        return (Integer) this.entityData.get(VehicleEntity.DATA_ID_HURT);
     }
 
     public int getHurtDir() {
-        return this.entityData.get(DATA_ID_HURTDIR);
+        return (Integer) this.entityData.get(VehicleEntity.DATA_ID_HURTDIR);
     }
 
     protected void destroy(DamageSource damageSource) {