mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-23 21:38:00 +01:00
Ensure tree types are available to the client when on a multiplayer server
This commit is contained in:
parent
e4eb233f7a
commit
b14771e85a
4 changed files with 65 additions and 94 deletions
|
@ -164,11 +164,11 @@ public interface TreeType {
|
|||
boolean isWide();
|
||||
|
||||
static TreeType at(BlockPos pos, World world) {
|
||||
return TreeTypeLoader.INSTANCE.get(world.getBlockState(pos), pos, world);
|
||||
return TreeTypes.get(world.getBlockState(pos), pos, world);
|
||||
}
|
||||
|
||||
static TreeType of(BlockState state) {
|
||||
return TreeTypeLoader.INSTANCE.get(state);
|
||||
return TreeTypes.get(state);
|
||||
}
|
||||
|
||||
static TreeType of(TreeType logs, TreeType leaves) {
|
||||
|
|
|
@ -1,147 +1,111 @@
|
|||
package com.minelittlepony.unicopia.ability.data.tree;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.minelittlepony.unicopia.Unicopia;
|
||||
import com.minelittlepony.unicopia.util.PosHelper;
|
||||
import com.minelittlepony.unicopia.util.Resources;
|
||||
import com.minelittlepony.unicopia.util.Weighted;
|
||||
|
||||
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.block.LeavesBlock;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.PacketByteBuf;
|
||||
import net.minecraft.resource.JsonDataLoader;
|
||||
import net.minecraft.resource.ResourceManager;
|
||||
import net.minecraft.tag.BlockTags;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.profiler.Profiler;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public class TreeTypeLoader extends JsonDataLoader implements IdentifiableResourceReloadListener {
|
||||
private static final Identifier ID = Unicopia.id("data/tree_type");
|
||||
|
||||
public static final TreeTypeLoader INSTANCE = new TreeTypeLoader();
|
||||
|
||||
private final Set<TreeType> entries = new HashSet<>();
|
||||
|
||||
private final TreeType any1x = createDynamic(false);
|
||||
private final TreeType any2x = createDynamic(true);
|
||||
private Map<Identifier, TreeTypeDef> entries = new HashMap<>();
|
||||
|
||||
TreeTypeLoader() {
|
||||
super(Resources.GSON, "tree_types");
|
||||
}
|
||||
|
||||
public Map<Identifier, TreeTypeDef> getEntries() {
|
||||
return entries;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Identifier getFabricId() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public TreeType get(BlockState state, BlockPos pos, World world) {
|
||||
return entries.stream()
|
||||
.filter(type -> type.matches(state))
|
||||
.findFirst()
|
||||
.map(type -> TreeType.of(type, type.findLeavesType(world, pos)))
|
||||
.orElseGet(() -> {
|
||||
if (any1x.matches(state)) {
|
||||
if (PosHelper.any(pos, p -> world.getBlockState(p).isOf(state.getBlock()), PosHelper.HORIZONTAL)) {
|
||||
return any2x;
|
||||
}
|
||||
|
||||
return any1x;
|
||||
}
|
||||
|
||||
return TreeType.NONE;
|
||||
});
|
||||
}
|
||||
|
||||
public TreeType get(BlockState state) {
|
||||
return entries.stream()
|
||||
.filter(type -> type.matches(state))
|
||||
.findFirst()
|
||||
.orElse(TreeType.NONE);
|
||||
}
|
||||
|
||||
private TreeType createDynamic(boolean wide) {
|
||||
return new TreeType() {
|
||||
@Override
|
||||
public boolean isLeaves(BlockState state) {
|
||||
return (state.isIn(BlockTags.LEAVES) || state.getBlock() instanceof LeavesBlock || entries.stream().anyMatch(t -> t.isLeaves(state))) && TreeTypeImpl.isNonPersistent(state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLog(BlockState state) {
|
||||
return state.isIn(BlockTags.LOGS_THAT_BURN) || entries.stream().anyMatch(t -> t.isLog(state));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ItemStack pickRandomStack(BlockState state) {
|
||||
TreeType type = get(state);
|
||||
if (type == TreeType.NONE) {
|
||||
type = get(Blocks.OAK_LOG.getDefaultState());
|
||||
}
|
||||
return type.pickRandomStack(state);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isWide() {
|
||||
return wide;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void apply(Map<Identifier, JsonElement> resources, ResourceManager manager, Profiler profiler) {
|
||||
entries.clear();
|
||||
|
||||
for (Map.Entry<Identifier, JsonElement> entry : resources.entrySet()) {
|
||||
entries = resources.entrySet().stream().filter(Objects::nonNull)
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, entry -> {
|
||||
try {
|
||||
TreeTypeDef typeDef = Resources.GSON.fromJson(entry.getValue(), TreeTypeDef.class);
|
||||
|
||||
if (typeDef != null) {
|
||||
entries.add(new TreeTypeImpl(
|
||||
entry.getKey(),
|
||||
typeDef.wideTrunk,
|
||||
typeDef.getWeighted(new Weighted<Supplier<ItemStack>>()),
|
||||
Objects.requireNonNull(typeDef.logs, "TreeType must have logs"),
|
||||
Objects.requireNonNull(typeDef.leaves, "TreeType must have leaves")
|
||||
));
|
||||
}
|
||||
return Resources.GSON.fromJson(entry.getValue(), TreeTypeDef.class);
|
||||
} catch (IllegalArgumentException | JsonParseException e) {
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}));
|
||||
TreeTypes.load(entries);
|
||||
}
|
||||
|
||||
static class TreeTypeDef {
|
||||
Set<Identifier> logs;
|
||||
Set<Identifier> leaves;
|
||||
Set<Drop> drops;
|
||||
boolean wideTrunk;
|
||||
public static final class TreeTypeDef {
|
||||
final Set<Identifier> logs;
|
||||
final Set<Identifier> leaves;
|
||||
final Set<Drop> drops;
|
||||
final boolean wideTrunk;
|
||||
|
||||
Weighted<Supplier<ItemStack>> getWeighted(Weighted<Supplier<ItemStack>> weighted) {
|
||||
public TreeTypeDef(PacketByteBuf buffer) {
|
||||
logs = new HashSet<>(buffer.readList(PacketByteBuf::readIdentifier));
|
||||
leaves = new HashSet<>(buffer.readList(PacketByteBuf::readIdentifier));
|
||||
drops = new HashSet<>(buffer.readList(Drop::new));
|
||||
wideTrunk = buffer.readBoolean();
|
||||
}
|
||||
|
||||
private Weighted<Supplier<ItemStack>> getWeighted(Weighted<Supplier<ItemStack>> weighted) {
|
||||
drops.forEach(drop -> drop.appendDrop(weighted));
|
||||
return weighted;
|
||||
}
|
||||
|
||||
public TreeType toTreeType(Identifier id) {
|
||||
return new TreeTypeImpl(
|
||||
id,
|
||||
wideTrunk,
|
||||
getWeighted(new Weighted<Supplier<ItemStack>>()),
|
||||
Objects.requireNonNull(logs, "TreeType must have logs"),
|
||||
Objects.requireNonNull(leaves, "TreeType must have leaves")
|
||||
);
|
||||
}
|
||||
|
||||
public void write(PacketByteBuf buffer) {
|
||||
buffer.writeCollection(logs, PacketByteBuf::writeIdentifier);
|
||||
buffer.writeCollection(leaves, PacketByteBuf::writeIdentifier);
|
||||
buffer.writeCollection(drops, (a, b) -> b.write(a));
|
||||
buffer.writeBoolean(wideTrunk);
|
||||
}
|
||||
|
||||
static class Drop {
|
||||
int weight;
|
||||
Identifier item;
|
||||
final int weight;
|
||||
final Identifier item;
|
||||
|
||||
public Drop(PacketByteBuf buffer) {
|
||||
weight = buffer.readInt();
|
||||
item = buffer.readIdentifier();
|
||||
}
|
||||
|
||||
void appendDrop(Weighted<Supplier<ItemStack>> weighted) {
|
||||
Registry.ITEM.getOrEmpty(item).ifPresent(item -> {
|
||||
weighted.put(weight, item::getDefaultStack);
|
||||
});
|
||||
}
|
||||
|
||||
public void write(PacketByteBuf buffer) {
|
||||
buffer.writeInt(weight);
|
||||
buffer.writeIdentifier(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.network;
|
|||
import java.util.*;
|
||||
|
||||
import com.minelittlepony.unicopia.InteractionManager;
|
||||
import com.minelittlepony.unicopia.ability.data.tree.TreeTypeLoader;
|
||||
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
|
||||
import com.minelittlepony.unicopia.client.gui.spellbook.ClientChapters;
|
||||
import com.minelittlepony.unicopia.container.SpellbookChapterLoader;
|
||||
|
@ -17,22 +18,26 @@ import net.minecraft.util.Identifier;
|
|||
public class MsgServerResources implements Packet<PlayerEntity> {
|
||||
public final Map<Identifier, SpellTraits> traits;
|
||||
public final Map<Identifier, ?> chapters;
|
||||
public final Map<Identifier, TreeTypeLoader.TreeTypeDef> treeTypes;
|
||||
|
||||
public MsgServerResources() {
|
||||
traits = SpellTraits.all();
|
||||
chapters = SpellbookChapterLoader.INSTANCE.getChapters();
|
||||
treeTypes = TreeTypeLoader.INSTANCE.getEntries();
|
||||
}
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public MsgServerResources(PacketByteBuf buffer) {
|
||||
traits = buffer.readMap(PacketByteBuf::readIdentifier, SpellTraits::fromPacket);
|
||||
chapters = buffer.readMap(PacketByteBuf::readIdentifier, ClientChapters::loadChapter);
|
||||
treeTypes = buffer.readMap(PacketByteBuf::readIdentifier, TreeTypeLoader.TreeTypeDef::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBuffer(PacketByteBuf buffer) {
|
||||
buffer.writeMap(traits, PacketByteBuf::writeIdentifier, (r, v) -> v.write(r));
|
||||
buffer.writeMap(chapters, PacketByteBuf::writeIdentifier, (r, v) -> ((SpellbookChapterLoader.Chapter)v).write(r));
|
||||
buffer.writeMap(treeTypes, PacketByteBuf::writeIdentifier, (r, v) -> v.write(r));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -5,6 +5,7 @@ import java.util.Map;
|
|||
import com.minelittlepony.unicopia.InteractionManager;
|
||||
import com.minelittlepony.unicopia.Owned;
|
||||
import com.minelittlepony.unicopia.USounds;
|
||||
import com.minelittlepony.unicopia.ability.data.tree.TreeTypes;
|
||||
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
|
||||
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
|
||||
import com.minelittlepony.unicopia.client.ClientBlockDestructionManager;
|
||||
|
@ -82,5 +83,6 @@ public class ClientNetworkHandlerImpl implements ClientNetworkHandler {
|
|||
public void handleServerResources(MsgServerResources packet) {
|
||||
SpellTraits.load(packet.traits);
|
||||
ClientChapters.load((Map<Identifier, Chapter>)packet.chapters);
|
||||
TreeTypes.load(packet.treeTypes);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue