simple-ish database + more improvements
All checks were successful
build / build (21) (push) Successful in 1m31s
All checks were successful
build / build (21) (push) Successful in 1m31s
This commit is contained in:
parent
1614e7ee49
commit
2f5084010a
|
@ -13,6 +13,7 @@ import ovh.sad.animalrp.commands.InteractionCommand;
|
|||
import ovh.sad.animalrp.commands.NoChatCommand;
|
||||
import ovh.sad.animalrp.commands.TfCommand;
|
||||
import ovh.sad.animalrp.util.Emote;
|
||||
import ovh.sad.animalrp.util.HashmapStore;
|
||||
import ovh.sad.animalrp.util.Mood;
|
||||
import eu.pb4.placeholders.api.PlaceholderContext;
|
||||
import eu.pb4.placeholders.api.PlaceholderHandler;
|
||||
|
@ -45,6 +46,13 @@ public class AnimalRP implements ModInitializer {
|
|||
animals.put("dog", new Dog());
|
||||
animals.put("fox", new Fox());
|
||||
animals.put("bee", new Bee());
|
||||
|
||||
HashmapStore.get("users.json").forEach((k,v) -> {
|
||||
users.put(UUID.fromString(k), animals.get(v));
|
||||
});
|
||||
HashmapStore.get("nochat.json").forEach((k,v) -> {
|
||||
noChat.put(UUID.fromString(k), Boolean.valueOf(v));
|
||||
});
|
||||
emotes = new Emote();
|
||||
|
||||
Placeholders.register(Identifier.of("animalrp", "animalcolor"), new PlaceholderHandler() {
|
||||
|
|
|
@ -14,6 +14,10 @@ public abstract class Animal {
|
|||
public HashMap<Mood, SoundEvent> moodSounds = new HashMap<Mood, SoundEvent>();
|
||||
public ArrayList<Item> superfoods = new ArrayList<Item>();
|
||||
|
||||
public String toString() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
Animal(String name, String catchphrase, String color) {
|
||||
this.name = name;
|
||||
this.catchphrase = catchphrase;
|
||||
|
|
|
@ -9,6 +9,7 @@ import net.minecraft.server.command.CommandManager;
|
|||
import net.minecraft.server.command.ServerCommandSource;
|
||||
import net.minecraft.text.Text;
|
||||
import ovh.sad.animalrp.AnimalRP;
|
||||
import ovh.sad.animalrp.util.HashmapStore;
|
||||
|
||||
public class NoChatCommand {
|
||||
public void Command(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess,
|
||||
|
@ -25,6 +26,7 @@ public class NoChatCommand {
|
|||
context.getSource().sendFeedback(() -> Text.literal("AnimalRP's chat modifications are now disabled for you.").withColor(16711680), false);
|
||||
AnimalRP.noChat.put(userUuid, true);
|
||||
}
|
||||
HashmapStore.save("nochat.json", AnimalRP.noChat);
|
||||
|
||||
return 0;
|
||||
}));
|
||||
|
|
|
@ -14,6 +14,7 @@ import net.minecraft.text.Text;
|
|||
import net.minecraft.util.Formatting;
|
||||
import ovh.sad.animalrp.AnimalRP;
|
||||
import ovh.sad.animalrp.animals.Animal;
|
||||
import ovh.sad.animalrp.util.HashmapStore;
|
||||
|
||||
public class TfCommand {
|
||||
public void Command(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess,
|
||||
|
@ -54,6 +55,7 @@ public class TfCommand {
|
|||
Integer.parseInt(animal.color.substring(1),
|
||||
16))),
|
||||
false);
|
||||
HashmapStore.save("users.json", AnimalRP.users);
|
||||
return 1;
|
||||
})));
|
||||
}
|
||||
|
|
|
@ -149,7 +149,6 @@ public class Emote {
|
|||
|
||||
public void playEmote(UUID uuid, ServerPlayerEntity entity, Emotes emote) {
|
||||
this.players.put(uuid, new PlayerEmote(entity, emote));
|
||||
|
||||
}
|
||||
|
||||
public void stopEmote(UUID uuid) {
|
||||
|
|
65
src/main/java/ovh/sad/animalrp/util/HashmapStore.java
Normal file
65
src/main/java/ovh/sad/animalrp/util/HashmapStore.java
Normal file
|
@ -0,0 +1,65 @@
|
|||
package ovh.sad.animalrp.util;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonIOException;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
import ovh.sad.animalrp.AnimalRP;
|
||||
|
||||
public class HashmapStore {
|
||||
static public Gson gson = new Gson();
|
||||
static public Path folder = FabricLoader.getInstance().getConfigDir().resolve(AnimalRP.MOD_ID).toAbsolutePath();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static public HashMap<String, String> get(String name) {
|
||||
Path filePath = folder.resolve(name);
|
||||
if (!Files.exists(filePath)) {
|
||||
return new HashMap<String, String>();
|
||||
}
|
||||
try {
|
||||
return gson.fromJson(new FileReader(filePath.toString()), HashMap.class);
|
||||
} catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return new HashMap<String, String>();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static public void save(String name, @SuppressWarnings("rawtypes") HashMap hashmap) {
|
||||
if (!Files.exists(folder)) {
|
||||
try {
|
||||
Files.createDirectory(folder);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Path filePath = folder.resolve(name);
|
||||
if (!Files.exists(filePath))
|
||||
try {
|
||||
Files.createFile(filePath);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
JsonObject jsobj = new JsonObject();
|
||||
hashmap.forEach((a, b) -> {
|
||||
jsobj.addProperty(a.toString(), b.toString());
|
||||
});
|
||||
|
||||
try {
|
||||
Files.write(filePath, gson.toJson(jsobj).getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue