Skip to content

Commit

Permalink
2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Mar 11, 2021
1 parent 4774903 commit ee65dc1
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/main/java/org/samo_lego/taterzens/Taterzens.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ public void onInitialize() {
public static Logger getLogger() {
return LOGGER;
}

/**
* Gets the minecraft Taterzens config directory.
* @return config directory folder.
*/
public static File getTaterDir() {
return taterDir;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, b
.then(argument("id", IntegerArgumentType.integer(1)).executes(NpcCommand::selectTaterzenById))
.executes(NpcCommand::selectTaterzen)
)
.then(literal("deselect").executes(NpcCommand::selectTaterzen))
.then(literal("deselect").executes(NpcCommand::deselectTaterzen))
.then(literal("list").executes(NpcCommand::listTaterzens))
.then(literal("remove").executes(NpcCommand::removeTaterzen))
.then(literal("preset")
Expand Down Expand Up @@ -139,6 +139,12 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher, b
);
}

private static int deselectTaterzen(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
((TaterzenEditor) context.getSource().getPlayer()).selectNpc(null);
context.getSource().sendFeedback(new LiteralText(lang.success.deselectedTaterzen).formatted(Formatting.GREEN), false);
return 0;
}

private static int deleteTaterzenMessage(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerPlayerEntity player = context.getSource().getPlayer();
TaterzenNPC taterzen = ((TaterzenEditor) player).getNpc();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package org.samo_lego.taterzens.mixin;

import net.fabricmc.fabric.impl.registry.sync.RegistrySyncManager;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.Packet;
import net.minecraft.network.PacketByteBuf;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import static org.samo_lego.taterzens.Taterzens.MODID;

@Mixin(RegistrySyncManager.class)
public class RegistrySyncManagerMixin_TaterzenSyncDisabler {
Expand All @@ -10,22 +19,20 @@ public class RegistrySyncManagerMixin_TaterzenSyncDisabler {
* Removes taterzen tag from registry sync, as we do not need it on client.
* Prevents client from being kicked if using FAPI.
*/
/*@Inject(
@Inject(
method = "createPacket",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/network/PacketByteBuf;writeCompoundTag(Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/network/PacketByteBuf;"
),
at = @At("TAIL"),
locals = LocalCapture.CAPTURE_FAILHARD,
remap = false
)
private static void removeTaterzenFromSync(CallbackInfoReturnable<Packet<?>> cir, CompoundTag tag, PacketByteBuf _buf) {
private static void removeTaterzenFromSync(CallbackInfoReturnable<Packet<?>> cir, CompoundTag tag, PacketByteBuf buf) {
CompoundTag registries = tag.getCompound("registries");
if(registries != null) {
CompoundTag entityTypes = registries.getCompound("minecraft:entity_type");
if(entityTypes != null) {
entityTypes.remove(MODID + ":npc");
buf.writeCompoundTag(tag);
}
}
}*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@
public class ServerPlayNetworkHandlerMixin_MsgEditor {
@Shadow public ServerPlayerEntity player;

/**
* Catches messages; if player is in
* message edit mode, messages sent to chat
* will be saved to taterzen instead.
*
* @param msg
* @param ci
*/
@Inject(
method = "method_31286(Ljava/lang/String;)V",
at = @At(
Expand All @@ -49,6 +57,7 @@ private void onMessage(String msg, CallbackInfo ci) {
} else {
Text text;
if(msg.startsWith("{") && msg.endsWith("}")) {
// NBT tellraw message structure, try parse it
try {
text = Text.Serializer.fromJson(new StringReader(msg));
} catch(JsonParseException ignored) {
Expand All @@ -59,6 +68,7 @@ private void onMessage(String msg, CallbackInfo ci) {
} else
text = new LiteralText(msg);
if(((TaterzenEditor) player).getMessageEditing() != -1) {
// Editing selected message
taterzen.setMessage(((TaterzenEditor) player).getMessageEditing(), text); // Editing message
player.sendMessage(TextUtil.successText(lang.success.messageChanged, text), false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@ public class ServerPlayerEntityMixinCast_TaterzenPlayer implements TaterzenPlaye
*/
@Unique
private long taterzens$lastNPCInteraction = 0;
/**
* Ticks since this player got last message from taterzen.
*/
@Unique
private int taterzens$lastMessageTicks = 0;
/**
* The last message index of the message that was sent
* to player.
*/
@Unique
private int taterzens$currentMsg = 0;

Expand Down Expand Up @@ -58,6 +65,10 @@ public void setCurrentMsgPos(int newPos) {
this.taterzens$currentMsg = newPos;
}

/**
* Increases the ticks since last message counter.
* @param ci
*/
@Inject(method = "tick()V", at = @At("TAIL"))
private void postTick(CallbackInfo ci) {
++this.taterzens$lastMessageTicks;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/samo_lego/taterzens/npc/NPCData.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class NPCData {
public EntityType<?> entityType = EntityType.PLAYER;
public boolean fakeTypeAlive = true;
/**
* Used for att
* Used for taterzen attributes.
*/
public boolean hostile = false;
public boolean leashable = config.defaults.leashable;
Expand Down
41 changes: 33 additions & 8 deletions src/main/java/org/samo_lego/taterzens/npc/TaterzenNPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,28 +596,53 @@ else if(!this.npcData.command.isEmpty()) {
return result;
}

/**
* Adds the message to taterzen's message list.
* @param text message to add
*/
public void addMessage(Text text) {
this.npcData.messages.add(new Pair<>(text, config.messages.messageDelay));
}

public void setMessage(int messageEditing, Text text) {
this.npcData.messages.set(messageEditing, new Pair<>(text, config.messages.messageDelay));
/**
* Edits the message from taterzen's message list at index.
* @param index index of the message to edit
* @param text
*/
public void setMessage(int index, Text text) {
if(index < this.npcData.messages.size())
this.npcData.messages.set(index, new Pair<>(text, config.messages.messageDelay));
}

public void removeMessage(int selected) {
this.npcData.messages.remove(selected);
/**
* Removes message at index.
* @param index index of message to be removed.
*/
public void removeMessage(int index) {
if(index < this.npcData.messages.size())
this.npcData.messages.remove(index);
}

/*public void setMessageDelay(int delay) {
if(!this.npcData.messages.isEmpty()) {
this.npcData.messages.get(this.npcData.currentMessage).mapSecond(previous -> delay);
/**
* Sets message delay.
*
* @param index index of the message to change delay for.
* @param delay new delay.
*/
public void setMessageDelay(int index, int delay) {
if(index < this.npcData.messages.size()) {
this.npcData.messages.get(index).mapSecond(previous -> delay);
}
}*/
}

public void clearMessages() {
this.npcData.messages = new ArrayList<>();
}

/**
* Gets {@link ArrayList} of {@link Pair}s of messages and their delays.
* @return arraylist of pairs with texts and delays.
*/
public ArrayList<Pair<Text, Integer>> getMessages() {
return this.npcData.messages;
}
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/samo_lego/taterzens/storage/TaterConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,35 @@ public class TaterConfig {
*
* Located at $minecraftFolder/config/Taterzens/$lang.json
*/
public String _comment_language = "// Language file to use.";
public String language = "en_us";
public Defaults defaults = new Defaults();
public Path path = new Path();
public Messages messages = new Messages();

/**
* Whether to remind you that if FabricTailor
* mod is installed, it has some more skin functionality
* (source: https://github.com/samolego/FabricTailor)
* mod is installed, it has some more skin functionality.
*
* @see <a href="https://github.com/samolego/FabricTailor">FabricTailor</a>
*/
public boolean fabricTailorAdvert = true;

public Defaults defaults = new Defaults();
public Path path = new Path();
public Messages messages = new Messages();

/**
* Default {@link org.samo_lego.taterzens.npc.TaterzenNPC} settings.
*/
public static class Defaults {
public String _comment = "// Default settings for new Taterzens.";
public String _comment_name = "// Default settings for new Taterzens.";
public String name = "Taterzen";
public boolean leashable = false;
public boolean pushable = false;
}

public static class Messages {
public String _commment = "// Default delay between each message, in ticks.";
public String _comment_messageDelay = "// Default delay between each message, in ticks.";
public int messageDelay = 100;
public String _comment = "// Whether to exit message editor mode after editing a message";
public String _comment_exitEditorAfterMsgEdit = "// Whether to exit message editor mode after editing a message.";
public boolean exitEditorAfterMsgEdit = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static class Success {
public String messageDelaySet = "Message delay for the last message is now %s.";
public String messageChanged = "Message has been changed to %s.";
public String messageDeleted = "Message %s has been deleted successfully.";
public String deselectedTaterzen = "Your Taterzen selection has been cleared.";
}

public static class Error {
Expand Down

0 comments on commit ee65dc1

Please sign in to comment.