Skip to content

Commit 179c7b6

Browse files
committed
Moved world stored/temp data into the API, functionally the data is the same since different worlds don't have different data.
1 parent c36ac28 commit 179c7b6

File tree

2 files changed

+98
-74
lines changed

2 files changed

+98
-74
lines changed

src/main/java/noppes/npcs/scripted/NpcAPI.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import net.minecraft.inventory.Container;
1919
import net.minecraft.inventory.IInventory;
2020
import net.minecraft.item.*;
21+
import net.minecraft.nbt.NBTBase;
2122
import net.minecraft.nbt.NBTTagCompound;
23+
import net.minecraft.nbt.NBTTagString;
2224
import net.minecraft.server.MinecraftServer;
2325
import net.minecraft.tileentity.TileEntity;
2426
import net.minecraft.util.DamageSource;
@@ -69,6 +71,7 @@
6971
import java.util.concurrent.Executors;
7072

7173
public class NpcAPI extends AbstractNpcAPI {
74+
private static final Map<String,Object> tempData = new HashMap<>();
7275
private static final Map<Integer, ScriptWorld> worldCache = new LRUHashMap<>(10);
7376
private static final CacheHashMap<ItemStack, CacheHashMap.CachedObject<ScriptItemStack>> scriptItemCache = new CacheHashMap<>(60*1000);
7477
public static final HashMap<String,Object> engineObjects = new HashMap<>();
@@ -86,6 +89,77 @@ public static void clearCache() {
8689
scriptItemCache.clear();
8790
}
8891

92+
public Object getTempData(String key){
93+
return tempData.get(key);
94+
}
95+
96+
public void setTempData(String key, Object value){
97+
tempData.put(key, value);
98+
}
99+
100+
public boolean hasTempData(String key){
101+
return tempData.containsKey(key);
102+
}
103+
104+
public void removeTempData(String key){
105+
tempData.remove(key);
106+
}
107+
108+
public void clearTempData(){
109+
tempData.clear();
110+
}
111+
112+
public String[] getTempDataKeys() {
113+
return tempData.keySet().toArray(new String[0]);
114+
}
115+
116+
public Object getStoredData(String key){
117+
NBTTagCompound compound = ScriptController.Instance.compound;
118+
if(!compound.hasKey(key))
119+
return null;
120+
NBTBase base = compound.getTag(key);
121+
if(base instanceof NBTBase.NBTPrimitive)
122+
return ((NBTBase.NBTPrimitive)base).func_150286_g();
123+
return ((NBTTagString)base).func_150285_a_();
124+
}
125+
126+
public void setStoredData(String key, Object value){
127+
NBTTagCompound compound = ScriptController.Instance.compound;
128+
if(value instanceof Number)
129+
compound.setDouble(key, ((Number) value).doubleValue());
130+
else if(value instanceof String)
131+
compound.setString(key, (String)value);
132+
ScriptController.Instance.shouldSave = true;
133+
}
134+
135+
public boolean hasStoredData(String key){
136+
return ScriptController.Instance.compound.hasKey(key);
137+
}
138+
139+
public void removeStoredData(String key){
140+
ScriptController.Instance.compound.removeTag(key);
141+
ScriptController.Instance.shouldSave = true;
142+
}
143+
144+
public void clearStoredData(){
145+
ScriptController.Instance.compound = new NBTTagCompound();
146+
ScriptController.Instance.shouldSave = true;
147+
}
148+
149+
public String[] getStoredDataKeys() {
150+
NBTTagCompound compound = ScriptController.Instance.compound;
151+
if (compound != null) {
152+
Set keySet = compound.func_150296_c();
153+
List<String> list = new ArrayList<>();
154+
for(Object o : keySet){
155+
list.add((String) o);
156+
}
157+
String[] array = list.toArray(new String[list.size()]);
158+
return array;
159+
}
160+
return new String[0];
161+
}
162+
89163
public void registerICommand(ICommand command) {
90164
((CommandHandler)CustomNpcs.getServer().getCommandManager()).registerCommand((ScriptedCommand) command);
91165
}

src/main/java/noppes/npcs/scripted/ScriptWorld.java

Lines changed: 24 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.*;
3939

4040
public class ScriptWorld implements IWorld {
41-
private static final Map<String,Object> tempData = new HashMap<>();
4241
public WorldServer world;
4342
public ScriptWorld(WorldServer world){
4443
this.world = world;
@@ -1159,113 +1158,64 @@ public IParticle createEntityParticle(String directory){
11591158
return NpcAPI.Instance().createEntityParticle(directory);
11601159
}
11611160

1162-
/**
1163-
* @param key Get temp data for this key
1164-
* @return Returns the stored temp data
1165-
*/
1161+
@Deprecated
11661162
public Object getTempData(String key){
1167-
return tempData.get(key);
1163+
return NpcAPI.Instance().getTempData(key);
11681164
}
11691165

1170-
/**
1171-
* Tempdata gets cleared when the server restarts. All worlds share the same temp data.
1172-
* @param key The key for the data stored
1173-
* @param value The data stored
1174-
*/
1166+
@Deprecated
11751167
public void setTempData(String key, Object value){
1176-
tempData.put(key, value);
1168+
NpcAPI.Instance().setTempData(key, value);
11771169
}
11781170

1179-
/**
1180-
* @param key The key thats going to be tested against the temp data
1181-
* @return Whether or not temp data containes the key
1182-
*/
1171+
@Deprecated
11831172
public boolean hasTempData(String key){
1184-
return tempData.containsKey(key);
1173+
return NpcAPI.Instance().hasTempData(key);
11851174
}
11861175

1187-
/**
1188-
* @param key The key for the temp data to be removed
1189-
*/
1176+
@Deprecated
11901177
public void removeTempData(String key){
1191-
tempData.remove(key);
1178+
NpcAPI.Instance().removeTempData(key);
11921179
}
11931180

1194-
/**
1195-
* Removes all tempdata
1196-
*/
1181+
@Deprecated
11971182
public void clearTempData(){
1198-
tempData.clear();
1183+
NpcAPI.Instance().clearTempData();
11991184
}
12001185

1186+
@Deprecated
12011187
public String[] getTempDataKeys() {
1202-
return tempData.keySet().toArray(new String[0]);
1188+
return NpcAPI.Instance().getTempDataKeys();
12031189
}
12041190

1205-
/**
1206-
* @param key The key of the data to be returned
1207-
* @return Returns the stored data
1208-
*/
1191+
@Deprecated
12091192
public Object getStoredData(String key){
1210-
NBTTagCompound compound = ScriptController.Instance.compound;
1211-
if(!compound.hasKey(key))
1212-
return null;
1213-
NBTBase base = compound.getTag(key);
1214-
if(base instanceof NBTPrimitive)
1215-
return ((NBTPrimitive)base).func_150286_g();
1216-
return ((NBTTagString)base).func_150285_a_();
1193+
return NpcAPI.Instance().getStoredData(key);
12171194
}
12181195

1219-
/**
1220-
* Stored data persists through world restart. Unlike tempdata only Strings and Numbers can be saved
1221-
* @param key The key for the data stored
1222-
* @param value The data stored. This data can be either a Number or a String. Other data is not stored
1223-
*/
1196+
@Deprecated
12241197
public void setStoredData(String key, Object value){
1225-
NBTTagCompound compound = ScriptController.Instance.compound;
1226-
if(value instanceof Number)
1227-
compound.setDouble(key, ((Number) value).doubleValue());
1228-
else if(value instanceof String)
1229-
compound.setString(key, (String)value);
1230-
ScriptController.Instance.shouldSave = true;
1198+
NpcAPI.Instance().setStoredData(key, value);
12311199
}
12321200

1233-
/**
1234-
* @param key The key of the data to be checked
1235-
* @return Returns whether or not the stored data contains the key
1236-
*/
1201+
@Deprecated
12371202
public boolean hasStoredData(String key){
1238-
return ScriptController.Instance.compound.hasKey(key);
1203+
return NpcAPI.Instance().hasStoredData(key);
12391204
}
12401205

1241-
/**
1242-
* @param key The key of the data to be removed
1243-
*/
1206+
@Deprecated
12441207
public void removeStoredData(String key){
1245-
ScriptController.Instance.compound.removeTag(key);
1246-
ScriptController.Instance.shouldSave = true;
1208+
NpcAPI.Instance().removeStoredData(key);
12471209
}
12481210

1249-
/**
1250-
* Remove all stored data
1251-
*/
1211+
@Deprecated
12521212
public void clearStoredData(){
1253-
ScriptController.Instance.compound = new NBTTagCompound();
1254-
ScriptController.Instance.shouldSave = true;
1213+
NpcAPI.Instance().clearStoredData();
12551214
}
12561215

1216+
@Deprecated
12571217
public String[] getStoredDataKeys() {
1258-
NBTTagCompound compound = ScriptController.Instance.compound;
1259-
if (compound != null) {
1260-
Set keySet = compound.func_150296_c();
1261-
List<String> list = new ArrayList<>();
1262-
for(Object o : keySet){
1263-
list.add((String) o);
1264-
}
1265-
String[] array = list.toArray(new String[list.size()]);
1266-
return array;
1267-
}
1268-
return new String[0];
1218+
return NpcAPI.Instance().getStoredDataKeys();
12691219
}
12701220

12711221
/**

0 commit comments

Comments
 (0)