summaryrefslogtreecommitdiffstats
path: root/nms-patches/TileEntityBeacon.patch
blob: 2b2bf7ed4a3b2e8a703da29d267bd4185f5218cc (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
157
--- a/net/minecraft/server/TileEntityBeacon.java
+++ b/net/minecraft/server/TileEntityBeacon.java
@@ -9,6 +9,13 @@
 import java.util.stream.Collectors;
 import javax.annotation.Nullable;
 
+// CraftBukkit start
+import org.bukkit.craftbukkit.entity.CraftHumanEntity;
+import org.bukkit.craftbukkit.potion.CraftPotionUtil;
+import org.bukkit.entity.HumanEntity;
+import org.bukkit.potion.PotionEffect;
+// CraftBukkit end
+
 public class TileEntityBeacon extends TileEntityContainer implements IWorldInventory, ITickable {
 
     public static final MobEffectList[][] a = new MobEffectList[][] { { MobEffects.FASTER_MOVEMENT, MobEffects.FASTER_DIG}, { MobEffects.RESISTANCE, MobEffects.JUMP}, { MobEffects.INCREASE_DAMAGE}, { MobEffects.REGENERATION}};
@@ -23,6 +30,38 @@
     public MobEffectList secondaryEffect;
     private ItemStack inventorySlot;
     private IChatBaseComponent o;
+    // CraftBukkit start - add fields and methods
+    public List<HumanEntity> transaction = new java.util.ArrayList<HumanEntity>();
+    private int maxStack = MAX_STACK;
+
+    public List<ItemStack> getContents() {
+        return Arrays.asList(this.inventorySlot);
+    }
+
+    public void onOpen(CraftHumanEntity who) {
+        transaction.add(who);
+    }
+
+    public void onClose(CraftHumanEntity who) {
+        transaction.remove(who);
+    }
+
+    public List<HumanEntity> getViewers() {
+        return transaction;
+    }
+
+    public void setMaxStackSize(int size) {
+        maxStack = size;
+    }
+
+    public PotionEffect getPrimaryEffect() {
+        return (this.primaryEffect != null) ? CraftPotionUtil.toBukkit(new MobEffect(this.primaryEffect, getLevel(), getAmplification(), true, true)) : null;
+    }
+
+    public PotionEffect getSecondaryEffect() {
+        return (hasSecondaryEffect()) ? CraftPotionUtil.toBukkit(new MobEffect(this.secondaryEffect, getLevel(), getAmplification(), true, true)) : null;
+    }
+    // CraftBukkit end
 
     public TileEntityBeacon() {
         super(TileEntityTypes.BEACON);
@@ -56,41 +95,79 @@
         this.world.a((EntityHuman) null, this.position, soundeffect, SoundCategory.BLOCKS, 1.0F, 1.0F);
     }
 
-    private void applyEffects() {
-        if (this.i && this.levels > 0 && !this.world.isClientSide && this.primaryEffect != null) {
-            double d0 = (double) (this.levels * 10 + 10);
+    // CraftBukkit start - split into components
+    private byte getAmplification() {
+        {
             byte b0 = 0;
 
             if (this.levels >= 4 && this.primaryEffect == this.secondaryEffect) {
                 b0 = 1;
             }
 
+            return b0;
+        }
+    }
+
+    private int getLevel() {
+        {
             int i = (9 + this.levels * 2) * 20;
+            return i;
+        }
+    }
+
+    public List getHumansInRange() {
+        {
+            double d0 = (double) (this.levels * 10 + 10);
+
             int j = this.position.getX();
             int k = this.position.getY();
             int l = this.position.getZ();
             AxisAlignedBB axisalignedbb = (new AxisAlignedBB((double) j, (double) k, (double) l, (double) (j + 1), (double) (k + 1), (double) (l + 1))).g(d0).b(0.0D, (double) this.world.getHeight(), 0.0D);
             List list = this.world.a(EntityHuman.class, axisalignedbb);
+
+            return list;
+        }
+    }
+
+    private void applyEffect(List list, MobEffectList effects, int i, int b0) {
+        {
             Iterator iterator = list.iterator();
 
             EntityHuman entityhuman;
 
             while (iterator.hasNext()) {
                 entityhuman = (EntityHuman) iterator.next();
-                entityhuman.addEffect(new MobEffect(this.primaryEffect, i, b0, true, true));
+                entityhuman.addEffect(new MobEffect(effects, i, b0, true, true), org.bukkit.event.entity.EntityPotionEffectEvent.Cause.BEACON);
             }
+        }
+    }
 
+    private boolean hasSecondaryEffect() {
+        {
             if (this.levels >= 4 && this.primaryEffect != this.secondaryEffect && this.secondaryEffect != null) {
-                iterator = list.iterator();
+                return true;
+            }
 
-                while (iterator.hasNext()) {
-                    entityhuman = (EntityHuman) iterator.next();
-                    entityhuman.addEffect(new MobEffect(this.secondaryEffect, i, 0, true, true));
-                }
+            return false;
+        }
+    }
+
+    private void applyEffects() {
+        if (this.i && this.levels > 0 && !this.world.isClientSide && this.primaryEffect != null) {
+            byte b0 = getAmplification();
+
+            int i = getLevel();
+            List list = getHumansInRange();
+
+            applyEffect(list, this.primaryEffect, i, b0);
+
+            if (hasSecondaryEffect()) {
+                applyEffect(list, this.secondaryEffect, i, 0);
             }
         }
 
     }
+    // CraftBukkit end
 
     private void E() {
         int i = this.position.getX();
@@ -210,8 +287,10 @@
 
     public void load(NBTTagCompound nbttagcompound) {
         super.load(nbttagcompound);
-        this.primaryEffect = e(nbttagcompound.getInt("Primary"));
-        this.secondaryEffect = e(nbttagcompound.getInt("Secondary"));
+        // Craftbukkit start - persist manually set non-default beacon effects (SPIGOT-3598)
+        this.primaryEffect = MobEffectList.fromId(nbttagcompound.getInt("Primary"));
+        this.secondaryEffect = MobEffectList.fromId(nbttagcompound.getInt("Secondary"));
+        // Craftbukkit end
         this.levels = nbttagcompound.getInt("Levels");
     }