-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCapabilityControllableSpawner.java
More file actions
173 lines (141 loc) · 6 KB
/
CapabilityControllableSpawner.java
File metadata and controls
173 lines (141 loc) · 6 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package ladysnake.spawnercontrol.controlledspawner;
import ladysnake.spawnercontrol.SpawnerControl;
import ladysnake.spawnercontrol.config.MSCConfig;
import ladysnake.spawnercontrol.config.SpawnerConfig;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.MobSpawnerBaseLogic;
import net.minecraft.tileentity.TileEntityMobSpawner;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.CapabilityInject;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.Constants;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Objects;
public class CapabilityControllableSpawner {
@CapabilityInject(IControllableSpawner.class)
public static Capability<IControllableSpawner> CAPABILITY_SPAWNER;
public static final ResourceLocation CAPABILITY_KEY = new ResourceLocation(SpawnerControl.MOD_ID, "controllable_spawner_cap");
public static IControllableSpawner getHandler(TileEntityMobSpawner entity) {
return Objects.requireNonNull(entity.getCapability(CAPABILITY_SPAWNER, null));
}
public static class DefaultControllableSpawner implements IControllableSpawner {
private final TileEntityMobSpawner spawner;
private int spawnedMobsCount;
private int aliveMobsCount;
public DefaultControllableSpawner() {
this(null);
}
DefaultControllableSpawner(TileEntityMobSpawner spawner) {
this.spawner = spawner;
}
@Override
public void setSpawnedMobsCount(int mobCount) {
this.spawnedMobsCount = mobCount;
}
@Override
public void setAliveMobsCount(int mobCount) {
this.aliveMobsCount = mobCount;
}
@Override
public boolean incrementSpawnedMobsCount() {
SpawnerConfig cfg = getConfig();
if(++this.spawnedMobsCount >= cfg.mobThreshold) {
if (cfg.breakSpawner)
spawner.getWorld().setBlockToAir(spawner.getPos());
return true;
}
this.adjustDelayAfterSpawn(spawner.getSpawnerBaseLogic(), cfg.spawnRateModifier);
return false;
}
@Override
public void incrementAliveMobsCount() {
++aliveMobsCount;
}
@Override
public void decrementAliveMobsCount() {
--aliveMobsCount;
}
/**
* Should be called after every spawn to tweak the cooldown according to the config
*/
protected void adjustDelayAfterSpawn(MobSpawnerBaseLogic spawnerBaseLogic, double spawnRateModifier) {
NBTTagCompound tags = new NBTTagCompound();
spawnerBaseLogic.writeToNBT(tags);
int minSpawnDelay = tags.getInteger("MinSpawnDelay");
int maxSpawnDelay = tags.getInteger("MaxSpawnDelay");
minSpawnDelay = (int) (minSpawnDelay*spawnRateModifier);
maxSpawnDelay = (int) (maxSpawnDelay*spawnRateModifier);
tags.setInteger("MinSpawnDelay",minSpawnDelay);
tags.setInteger("MaxSpawnDelay",maxSpawnDelay);
spawnerBaseLogic.readFromNBT(tags);
}
@Override
public int getSpawnedMobsCount() {
return spawnedMobsCount;
}
@Override
public int getAliveMobsCount() {
return aliveMobsCount;
}
@Override
public boolean canSpawn() {
return this.spawnedMobsCount < getConfig().mobThreshold;
}
@Override
public boolean tooManyAlive() {
if(getConfig().pauseIfTooManyAlive)
return this.aliveMobsCount >= getConfig().aliveMobThreshold;
else
return false;
}
@Nonnull
@Override
public SpawnerConfig getConfig() {
return MSCConfig.vanillaSpawnerConfig;
}
}
public static class Provider implements ICapabilitySerializable<NBTTagCompound> {
final IControllableSpawner instance;
public Provider(TileEntityMobSpawner spawner) {
this.instance = new DefaultControllableSpawner(spawner);
}
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == CAPABILITY_SPAWNER;
}
@Nullable
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
return capability == CAPABILITY_SPAWNER ? CAPABILITY_SPAWNER.cast(instance) : null;
}
@Override
public NBTTagCompound serializeNBT() {
return (NBTTagCompound) CAPABILITY_SPAWNER.getStorage().writeNBT(CAPABILITY_SPAWNER, instance, null);
}
@Override
public void deserializeNBT(NBTTagCompound nbt) {
CAPABILITY_SPAWNER.getStorage().readNBT(CAPABILITY_SPAWNER, instance, null, nbt);
}
}
public static class Storage implements Capability.IStorage<IControllableSpawner> {
@Nullable
@Override
public NBTBase writeNBT(Capability<IControllableSpawner> capability, IControllableSpawner instance, EnumFacing side) {
NBTTagCompound nbt = new NBTTagCompound();
nbt.setInteger("SpawnedMobsCount", instance.getSpawnedMobsCount());
nbt.setInteger("AliveMobsCount", instance.getAliveMobsCount());
return nbt;
}
@Override
public void readNBT(Capability<IControllableSpawner> capability, IControllableSpawner instance, EnumFacing side, NBTBase nbt) {
if (nbt instanceof NBTTagCompound) {
instance.setSpawnedMobsCount(((NBTTagCompound) nbt).getInteger("SpawnedMobsCount"));
instance.setAliveMobsCount(((NBTTagCompound) nbt).getInteger("AliveMobsCount"));
}
}
}
}