-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: 秋雨落 <[email protected]>
- Loading branch information
Showing
12 changed files
with
193 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/cuteneko/catsplus/listener/CatSpiritListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters