Skip to content

Commit

Permalink
Add cat spirit.
Browse files Browse the repository at this point in the history
Signed-off-by: 秋雨落 <[email protected]>
  • Loading branch information
qyl27 committed Feb 24, 2024
1 parent fad8c3e commit 5110fe1
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void onInitializeClient() {
mod.initClient();

ColorProviderRegistry.ITEM.register(
((stack, tintIndex) -> tintIndex > 0 ? -1 : ((DyeableItem) stack.getItem()).getColor(stack)),
(stack, tintIndex) -> tintIndex == 0 ? ((DyeableItem) stack.getItem()).getColor(stack) : -1,
ModItems.CAT_BAG.get()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ public void generateTranslations(TranslationBuilder builder) {
builder.add(Constants.MESSAGE_CAT_SPIRIT_DESCRIPTION_TIME_LABEL, "Dead at: ");
builder.add(Constants.MESSAGE_CAT_SPIRIT_DESCRIPTION_TIME_PATTERN, "%2$s/%3$s/%1$s %4$s:%5$s:%6$s");
builder.add(Constants.MESSAGE_CAT_SPIRIT_DESCRIPTION_TIME_LONG_TIME_AGO, "Long time ago");

builder.add(Constants.MESSAGE_CAT_DIED, "Your cat was died, here is its spirit.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ public void generateTranslations(TranslationBuilder builder) {
builder.add(Constants.MESSAGE_CAT_SPIRIT_DESCRIPTION_TIME_LABEL, "卒于:");
builder.add(Constants.MESSAGE_CAT_SPIRIT_DESCRIPTION_TIME_PATTERN, "%1$s年%2$s月%3$s日 %4$s:%5$s:%6$s");
builder.add(Constants.MESSAGE_CAT_SPIRIT_DESCRIPTION_TIME_LONG_TIME_AGO, "很久以前");

builder.add(Constants.MESSAGE_CAT_DIED, "你的猫猫死掉了,它的灵魂回到了你的身旁。");
}
}
3 changes: 3 additions & 0 deletions src/main/java/cuteneko/catsplus/CatsPlus.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import cuteneko.catsplus.effect.potion.ModPotions;
import cuteneko.catsplus.item.ModItems;
import cuteneko.catsplus.item.group.ModItemGroups;
import cuteneko.catsplus.listener.CatSpiritListener;
import cuteneko.catsplus.listener.ModListeners;
import cuteneko.catsplus.utility.Constants;
import dev.architectury.registry.item.ItemPropertiesRegistry;
import net.minecraft.util.Identifier;
Expand All @@ -30,6 +32,7 @@ public void init() {
ModItems.register();
ModEffects.register();
ModPotions.register();
ModListeners.register();
}

public void initClient() {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/cuteneko/catsplus/CatsPlusData.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package cuteneko.catsplus;

import cuteneko.catsplus.data.ICatSpirit;
import cuteneko.catsplus.data.*;
import cuteneko.catsplus.data.impl.CatServer;
import cuteneko.catsplus.data.impl.CatSpirit;
import cuteneko.catsplus.item.ModItems;
import cuteneko.catsplus.data.ICatBag;
import cuteneko.catsplus.data.ICatPlayer;
import cuteneko.catsplus.data.IGeniusCat;
import cuteneko.catsplus.data.impl.CatBag;
import cuteneko.catsplus.utility.Constants;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.entity.passive.CatEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.World;

import java.util.Objects;

public class CatsPlusData {
@ExpectPlatform
Expand Down Expand Up @@ -38,4 +41,9 @@ public static ICatSpirit getCatSpirit(ItemStack spirit) {

return new CatSpirit(spirit);
}

public static ICatServer getCatServer(MinecraftServer server) {
return Objects.requireNonNull(server.getWorld(World.OVERWORLD)).getPersistentStateManager()
.getOrCreate(CatServer.TYPE, Constants.TAG_SERVER_HAS_CAT.toString());
}
}
28 changes: 28 additions & 0 deletions src/main/java/cuteneko/catsplus/data/ICatServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cuteneko.catsplus.data;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.collection.DefaultedList;

import java.util.Map;
import java.util.UUID;

public interface ICatServer {
Map<UUID, DefaultedList<ItemStack>> getCatSpirits();

DefaultedList<ItemStack> getCatSpiritsByOwner(UUID uuid);

default DefaultedList<ItemStack> getCatSpiritsByOwner(PlayerEntity player) {
return getCatSpiritsByOwner(player.getUuid());
}

void addCatSpirit(UUID uuid, ItemStack catSpirit);

void clearCatSpirits();

void clearCatSpiritsByOwner(UUID uuid);

default void clearCatSpiritsByOwner(PlayerEntity player) {
clearCatSpiritsByOwner(player.getUuid());
}
}
86 changes: 86 additions & 0 deletions src/main/java/cuteneko/catsplus/data/impl/CatServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cuteneko.catsplus.data.impl;

import com.google.common.collect.ImmutableMap;
import cuteneko.catsplus.data.ICatServer;
import cuteneko.catsplus.utility.Constants;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.PersistentState;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class CatServer extends PersistentState implements ICatServer {

// Todo: qyl27: DataFixerUpper.
public static final Type<CatServer> TYPE = new Type<>(CatServer::new, CatServer::loadFromNbt, null);

private final Map<UUID, DefaultedList<ItemStack>> catSpirits = new HashMap<>();

@Override
public Map<UUID, DefaultedList<ItemStack>> getCatSpirits() {
return ImmutableMap.<UUID, DefaultedList<ItemStack>>builder().putAll(catSpirits).build();
}

@Override
public DefaultedList<ItemStack> getCatSpiritsByOwner(UUID uuid) {
if (!catSpirits.containsKey(uuid)) {
catSpirits.put(uuid, DefaultedList.of());
}

return catSpirits.get(uuid);
}

@Override
public void addCatSpirit(UUID uuid, ItemStack catSpirit) {
if (uuid == null) {
return;
}

var list = getCatSpiritsByOwner(uuid);
list.add(catSpirit);
}

@Override
public void clearCatSpirits() {
catSpirits.clear();
}

@Override
public void clearCatSpiritsByOwner(UUID uuid) {
getCatSpiritsByOwner(uuid).clear();
}

@Override
public NbtCompound writeNbt(NbtCompound nbt) {
var list = new NbtList();
for (var entry : getCatSpirits().entrySet()) {
var e = new NbtCompound();
e.putUuid(Constants.TAG_UUID, entry.getKey());
e.put(Constants.TAG_VALUE, Inventories.writeNbt(new NbtCompound(), entry.getValue()));
}
nbt.put(Constants.TAG_SERVER_CAT_SPIRITS, list);
return nbt;
}

public static CatServer loadFromNbt(NbtCompound nbt) {
var result = new CatServer();

var spirits = nbt.getList(Constants.TAG_SERVER_CAT_SPIRITS, NbtElement.COMPOUND_TYPE);
for (var spirit : spirits) {
if (spirit instanceof NbtCompound compound) {
var uuid = compound.getUuid(Constants.TAG_UUID);
var value = DefaultedList.<ItemStack>of();
Inventories.readNbt(compound.getCompound(Constants.TAG_VALUE), value);
result.catSpirits.put(uuid, value);
}
}

return result;
}
}
4 changes: 3 additions & 1 deletion src/main/java/cuteneko/catsplus/item/CatSpiritItem.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cuteneko.catsplus.item;

import cuteneko.catsplus.CatsPlusData;
import cuteneko.catsplus.item.group.ModItemGroups;
import cuteneko.catsplus.utility.Constants;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
Expand All @@ -12,7 +13,8 @@ public CatSpiritItem() {
super(new Item.Settings()
.maxCount(1)
.fireproof()
.rarity(Rarity.EPIC));
.rarity(Rarity.EPIC)
.arch$tab(ModItemGroups.CATS_PLUS));
}

@Override
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/cuteneko/catsplus/listener/CatSpiritListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cuteneko.catsplus.listener;

import cuteneko.catsplus.CatsPlusData;
import cuteneko.catsplus.utility.Constants;
import dev.architectury.event.events.common.PlayerEvent;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

public class CatSpiritListener {
public CatSpiritListener() {
// TickEvent.PLAYER_POST.register(this::onPlayerTick);
PlayerEvent.PLAYER_JOIN.register(this::onPlayerJoin);
}

private void onPlayerJoin(ServerPlayerEntity player) {
var catServer = CatsPlusData.getCatServer(player.server);
var spirits = catServer.getCatSpiritsByOwner(player);
for (var spirit : spirits) {
player.giveItemStack(spirit);
player.sendMessage(Text.translatable(Constants.MESSAGE_CAT_DIED));
}
catServer.clearCatSpiritsByOwner(player);
}
}
7 changes: 7 additions & 0 deletions src/main/java/cuteneko/catsplus/listener/ModListeners.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cuteneko.catsplus.listener;

public class ModListeners {
public static void register() {
new CatSpiritListener();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cuteneko.catsplus.CatsPlusData;
import cuteneko.catsplus.item.ModItems;
import cuteneko.catsplus.utility.Constants;
import cuteneko.catsplus.utility.GeniusCatHelper;
import net.minecraft.entity.EntityStatuses;
import net.minecraft.entity.EntityType;
Expand All @@ -12,6 +13,8 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -39,17 +42,26 @@ private void beforeHandleStatus(byte status, CallbackInfo ci) {
@Inject(method = "onDeath", at = @At("RETURN"))
private void afterDeath(DamageSource damageSource, CallbackInfo ci) {
if ((Object) this instanceof CatEntity cat) {
var owner = cat.getOwner();
if (owner instanceof PlayerEntity player) {
var stack = new ItemStack(ModItems.CAT_SPIRIT);
var spirit = CatsPlusData.getCatSpirit(stack);
if (cat.getOwnerUuid() == null) {
return;
}

var stack = new ItemStack(ModItems.CAT_SPIRIT);
var spirit = CatsPlusData.getCatSpirit(stack);

spirit.setCat(cat);
spirit.setDeathTime(OffsetDateTime.now());
spirit.setDeathMessage(this.getDamageTracker().getDeathMessage());
spirit.setCat(cat);
spirit.setDeathTime(OffsetDateTime.now());
spirit.setDeathMessage(getDamageTracker().getDeathMessage());

var owner = cat.getOwner();
if (owner == null) {
CatsPlusData.getCatServer(cat.getServer()).addCatSpirit(cat.getOwnerUuid(), stack);
return;
}

// Todo: qyl27: how about player offline?
// player.giveItemStack(stack);
if (owner instanceof ServerPlayerEntity player) {
player.giveItemStack(stack);
player.sendMessage(Text.translatable(Constants.MESSAGE_CAT_DIED));
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cuteneko/catsplus/utility/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class Constants {
public static final String TAG_CAT_PLAYER_INNER_CAT = "innerCat";
public static final String TAG_CAT_PLAYER_IS_CAT = "isCat";

public static final Identifier TAG_SERVER_HAS_CAT = new Identifier(CatsPlus.MODID, "cat_server_data");
public static final String TAG_SERVER_CAT_SPIRITS = "catSpirits";

public static final Identifier CAP_GENIUS_CAT = new Identifier(CatsPlus.MODID, "genius_cat_data");
public static final String TAG_GENIUS_CAT_LIVES = "lives";
public static final String TAG_GENIUS_CAT_TOTEM = "totem";
Expand Down Expand Up @@ -48,4 +51,5 @@ public class Constants {
public static final String MESSAGE_CATTIFY_LINGERING_POTION = "item.minecraft.lingering_potion.effect.cattify";
public static final String MESSAGE_CATTIFY_POTION_ARROW = "item.minecraft.tipped_arrow.effect.cattify";

public static final String MESSAGE_CAT_DIED = "message.catsplus.cat_died";
}

0 comments on commit 5110fe1

Please sign in to comment.