Skip to content

Commit

Permalink
Support for loading from spawn eggs
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Jan 2, 2022
1 parent 63113aa commit ca79acd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 32 deletions.
20 changes: 17 additions & 3 deletions common/src/main/java/org/samo_lego/taterzens/api/TaterzensAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@
import org.samo_lego.taterzens.api.professions.TaterzenProfession;
import org.samo_lego.taterzens.npc.TaterzenNPC;

import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

import static org.samo_lego.taterzens.Taterzens.*;
import static org.samo_lego.taterzens.Taterzens.GSON;
import static org.samo_lego.taterzens.Taterzens.LEGACY_PROFESSION_TYPES;
import static org.samo_lego.taterzens.Taterzens.LOGGER;
import static org.samo_lego.taterzens.Taterzens.MODID;
import static org.samo_lego.taterzens.Taterzens.PROFESSION_TYPES;
import static org.samo_lego.taterzens.Taterzens.TATERZEN_TYPE;
import static org.samo_lego.taterzens.Taterzens.presetsDir;

/**
* Class containing static methods to use with Taterzens.
Expand All @@ -46,8 +59,9 @@ public class TaterzensAPI {
@Nullable
public static TaterzenNPC loadTaterzenFromPreset(File preset, Level world) {
if (preset.exists()) {
String name = preset.getName();
TaterzenNPC taterzenNPC = new TaterzenNPC(TATERZEN_TYPE, world);
taterzenNPC.loadFromPresetFile(preset);
taterzenNPC.loadFromPresetFile(preset, name.substring(0, name.lastIndexOf('.')));

return taterzenNPC;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@

public class LockCommand {
public static void registerNode(LiteralCommandNode<CommandSourceStack> npcNode) {
LiteralCommandNode<CommandSourceStack> lockingNode = literal("action")
.then(literal("lock")
.requires(src -> permissions$checkPermission(src, "taterzens.npc.lock", config.perms.npcCommandPermissionLevel))
.executes(context -> lock(context, true))
)
.then(literal("unclock")
.requires(src -> permissions$checkPermission(src, "taterzens.npc.unlock", config.perms.npcCommandPermissionLevel))
.executes(context -> lock(context, false))
)
LiteralCommandNode<CommandSourceStack> lockingNode = literal("lock")
.requires(src -> permissions$checkPermission(src, "taterzens.npc.lock", config.perms.npcCommandPermissionLevel))
.executes(context -> lock(context, true))
.build();


LiteralCommandNode<CommandSourceStack> unlockingNode = literal("unlock")
.requires(src -> permissions$checkPermission(src, "taterzens.npc.unlock", config.perms.npcCommandPermissionLevel))
.executes(context -> lock(context, false))
.build();

npcNode.addChild(lockingNode);
npcNode.addChild(unlockingNode);
}

private static int lock(CommandContext<CommandSourceStack> context, boolean lock) throws CommandSyntaxException {
CommandSourceStack source = context.getSource();
Entity entity = source.getEntityOrException();
return NpcCommand.selectedTaterzenExecutor(entity, taterzen -> {
if (!taterzen.isLocked() && lock) {
if (lock) {
taterzen.setLocked(entity);
source.sendSuccess(
successText("taterzens.command.lock.success", taterzen.getName().getString()),
false
);
} else if (taterzen.canEdit(entity) && !lock) {
} else {
source.sendSuccess(
successText("taterzens.command.unlock.success", taterzen.getName().getString()),
false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mojang.authlib.GameProfile;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import org.samo_lego.taterzens.npc.TaterzenNPC;
import xyz.nucleoid.disguiselib.casts.EntityDisguise;

Expand All @@ -13,10 +12,6 @@
*/
public class DisguiseLibCompatibility {

public static void disguiseAs(TaterzenNPC taterzen, EntityType<?> entityType) {
((EntityDisguise) taterzen).disguiseAs(entityType);
}

public static void disguiseAs(TaterzenNPC taterzen, Entity entity) {
((EntityDisguise) taterzen).disguiseAs(entity);
}
Expand All @@ -25,10 +20,6 @@ public static void setGameProfile(TaterzenNPC taterzen, GameProfile gameProfile)
((EntityDisguise) taterzen).setGameProfile(gameProfile);
}

public static boolean isDisguised(TaterzenNPC taterzen) {
return ((EntityDisguise) taterzen).isDisguised();
}

public static void clearDisguise(TaterzenNPC taterzen) {
((EntityDisguise) taterzen).removeDisguise();
}
Expand Down
51 changes: 42 additions & 9 deletions common/src/main/java/org/samo_lego/taterzens/npc/TaterzenNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,11 @@ public void readAdditionalSaveData(CompoundTag tag) {

// Has a "preset" tag
// We want to override other data
if (!tag.getString("PresetOverride").isEmpty()) {
System.out.println("Has additional save data!");
if (tag.contains("PresetOverride")) {
this.loadPresetTag(tag);
return; // Other data doesn't need to be loaded as it will be handled by preset
}

System.out.println("Loading!");

CompoundTag npcTag = tag.getCompound("TaterzenNPCTag");

CompoundTag tags = npcTag.getCompound("Tags");
Expand Down Expand Up @@ -771,6 +768,9 @@ public void readAdditionalSaveData(CompoundTag tag) {
this.setMovement(NPCData.Movement.valueOf(npcTag.getString("movement")));
else
this.setMovement(NPCData.Movement.NONE);

if (npcTag.contains("LockedBy"))
this.lockedUuid = npcTag.getUUID("LockedBy");
}

/**
Expand Down Expand Up @@ -880,6 +880,10 @@ public void addAdditionalSaveData(CompoundTag tag) {
bodyRotations.putFloat("YRot", this.getYRot());
npcTag.put("BodyRotations", bodyRotations);

// Locking
if (this.lockedUuid != null)
npcTag.putUUID("LockedBy", this.lockedUuid);

tag.put("TaterzenNPCTag", npcTag);
}

Expand All @@ -891,21 +895,28 @@ private void loadPresetTag(CompoundTag tag) {
String preset = tag.getString("PresetOverride") + ".json";
File presetFile = new File(presetsDir + "/" + preset);

System.out.println(presetFile.exists());

if (presetFile.exists())
this.loadFromPresetFile(presetFile);
this.loadFromPresetFile(presetFile, preset);
}

/**
* Loads Taterzen data from preset file. Loads team data as well.
* @param presetFile file containing a taterzen preset.
* @param presetName name of the preset.
*/
public void loadFromPresetFile(File presetFile) {
public void loadFromPresetFile(File presetFile, String presetName) {
CompoundTag saveTag = TaterzensAPI.loadPresetTag(presetFile);
saveTag.putString("UUID", this.getStringUUID());
saveTag.remove("PresetOverride"); // Avoid looping if user has messed with preset

// Avoid looping if user has messed with preset
if (!presetName.isEmpty() && presetName.equals(saveTag.getString("PresetOverride"))) {
saveTag.remove("PresetOverride");
LOGGER.warn("Preset override loop detected in {}. Aborting it.", presetName);
}

Vec3 savedPos = this.getPosition(0);
this.load(saveTag);
this.setPos(savedPos);

CompoundTag npcTag = (CompoundTag) saveTag.get("TaterzenNPCTag");
if (npcTag != null) {
Expand Down Expand Up @@ -1687,22 +1698,44 @@ public void travel(Vec3 vec3) {
}
}

/**
* Whether taterzen should be allowed to be edited by entity.
* @param entity entity to check.
* @return true if taterzen can be edited by entity, otherwise false.
*/
public boolean canEdit(Entity entity) {
return this.canEdit(entity.getUUID());
}

/**
* Whether taterzen should be allowed to be edited by provided uuid.
* @param uuid uuid to check.
* @return true if taterzen can be edited by provided uuid, otherwise false.
*/
public boolean canEdit(UUID uuid) {
return this.lockedUuid == null || this.lockedUuid.equals(uuid) || this.getUUID().equals(uuid);
}

/**
* Whether taterzen is locked.
* @return true if taterzen is locked, otherwise false.
*/
public boolean isLocked() {
return this.lockedUuid != null;
}

/**
* Sets taterzen to be locked by provided owner's uuid.
* @param owner entity to lock taterzen to.
*/
public void setLocked(Entity owner) {
this.setLocked(owner.getUUID());
}

/**
* Sets taterzen to be locked by provided uuid.
* @param uuid uuid to lock taterzen to.
*/
public void setLocked(UUID uuid) {
this.lockedUuid = uuid;
}
Expand Down

0 comments on commit ca79acd

Please sign in to comment.