Skip to content

Commit

Permalink
Move some internals around, add support for opening creative item gro…
Browse files Browse the repository at this point in the history
…ups with commands
  • Loading branch information
Patbox committed Nov 22, 2021
1 parent 8c7a771 commit 4ce613b
Show file tree
Hide file tree
Showing 37 changed files with 686 additions and 199 deletions.
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.18-pre5
yarn_mappings=1.18-pre5+build.1
minecraft_version=1.18-pre6
yarn_mappings=1.18-pre6+build.1
loader_version=0.12.5

#Fabric api
fabric_version=0.42.6+1.18

# Mod Properties
mod_version = 0.2.0-alpha.4
mod_version = 0.2.0-alpha.5
maven_group = eu.pb4
archives_base_name = polymer

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.pb4.polymer.api.client.registry;

import eu.pb4.polymer.api.utils.PolymerRegistry;
import eu.pb4.polymer.api.utils.PolymerUtils;
import eu.pb4.polymer.impl.PolymerImpl;
import eu.pb4.polymer.impl.client.InternalClientRegistry;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
Expand All @@ -13,7 +13,7 @@
import java.util.Map;

public record ClientPolymerBlock(Identifier identifier, int numId, Text name, BlockState defaultBlockState) {
public static final ClientPolymerBlock NONE = new ClientPolymerBlock(PolymerUtils.id("none"), 0, LiteralText.EMPTY, Blocks.AIR.getDefaultState());
public static final ClientPolymerBlock NONE = new ClientPolymerBlock(PolymerImpl.id("none"), 0, LiteralText.EMPTY, Blocks.AIR.getDefaultState());
public static final State NONE_STATE = new State(Collections.emptyMap(), NONE);

public static final PolymerRegistry<ClientPolymerBlock> REGISTRY = InternalClientRegistry.BLOCKS;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/eu/pb4/polymer/api/item/PolymerItemGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class PolymerItemGroup extends ItemGroup implements PolymerObject {
/**
* Even called on synchronization of PolymerItemGroups
*/
public static final SimpleEvent<ItemGroupSyncEventListener> SYNC_EVENT = new SimpleEvent<>();
public static final SimpleEvent<ItemGroupListEventListener> LIST_EVENT = new SimpleEvent<>();
public static List<ItemStack> items = new ArrayList<>();
private final Text name;
private final Identifier identifier;
Expand Down Expand Up @@ -94,7 +94,7 @@ public boolean shouldSyncWithPolymerClient(ServerPlayerEntity player) {
}

@FunctionalInterface
public interface ItemGroupSyncEventListener {
public interface ItemGroupListEventListener {
void onItemGroupSync(ServerPlayerEntity player, ItemGroupSyncer helper);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eu.pb4.polymer.api.resourcepack.PolymerRPUtils;
import eu.pb4.polymer.api.utils.PolymerObject;
import eu.pb4.polymer.api.utils.PolymerUtils;
import eu.pb4.polymer.impl.PolymerImpl;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvent;
import net.minecraft.util.Identifier;
Expand All @@ -13,7 +14,7 @@
* It can be used to play custom sounds for players with resourcepack while keeping fallback for vanilla clients
*/
public class PolymerSoundEvent extends SoundEvent implements PolymerObject {
public static final SoundEvent EMPTY_SOUND = new SoundEvent(PolymerUtils.id("empty_sound"));
public static final SoundEvent EMPTY_SOUND = new SoundEvent(PolymerImpl.id("empty_sound"));
private final SoundEvent polymerSound;

public PolymerSoundEvent(Identifier id, @Nullable SoundEvent vanillaEvent) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/eu/pb4/polymer/api/other/PolymerStat.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private PolymerStat(String id) {
* @param formatter the formatter for the stat to use
* @return the PolymerStat ({@link Identifier}) for the custom stat
*/
public static PolymerStat registerStat(String id, StatFormatter formatter) {
public static Identifier registerStat(String id, StatFormatter formatter) {
PolymerStat identifier = new PolymerStat(id);
Registry.register(Registry.CUSTOM_STAT, id, identifier);
Stats.CUSTOM.getOrCreateStat(identifier, formatter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package eu.pb4.polymer.api.resourcepack;


import org.jetbrains.annotations.ApiStatus;

import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;

public interface PolymerRPBuilder {
boolean addData(String path, byte[] data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.pb4.polymer.api.resourcepack;

import eu.pb4.polymer.api.utils.events.SimpleEvent;
import eu.pb4.polymer.impl.PolymerGlobalValues;
import eu.pb4.polymer.impl.PolymerImpl;
import eu.pb4.polymer.impl.PolymerMod;
import eu.pb4.polymer.impl.client.ClientUtils;
import eu.pb4.polymer.impl.compat.CompatStatus;
Expand Down Expand Up @@ -32,7 +32,7 @@ public class PolymerRPUtils {
private static final Map<Identifier, PolymerArmorModel> ARMOR_MODEL_MAP = new HashMap<>();
private static int ARMOR_VAL = 0;
private static final int CMD_OFFSET = CompatStatus.POLYMC ? 100000 : 1;
private static boolean REQUIRED = PolymerGlobalValues.FORCE_RESOURCE_PACK_SERVER;
private static boolean REQUIRED = PolymerImpl.FORCE_RESOURCE_PACK_SERVER;
private static boolean DEFAULT_CHECK = true;

/**
Expand Down Expand Up @@ -191,7 +191,7 @@ public static boolean build(Path output) {

return successful;
} catch (Exception e) {
PolymerMod.LOGGER.error("Couldn't create resource pack!");
PolymerImpl.LOGGER.error("Couldn't create resource pack!");
e.printStackTrace();
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/eu/pb4/polymer/api/utils/PolymerObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ public interface PolymerObject {
default boolean shouldSyncWithPolymerClient(ServerPlayerEntity player) {
return true;
}

static boolean is(Object obj) {
return obj instanceof PolymerObject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public interface PolymerRegistry<T> extends Iterable<T> {
T get(int id);
Identifier getId(T entry);
int getRawId(T entry);

Iterable<Identifier> ids();
int size();
}
32 changes: 29 additions & 3 deletions src/main/java/eu/pb4/polymer/api/utils/PolymerUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.pb4.polymer.api.utils;

import eu.pb4.polymer.api.item.PolymerItemGroup;
import eu.pb4.polymer.api.networking.PolymerSyncUtils;
import eu.pb4.polymer.impl.InternalServerRegistry;
import eu.pb4.polymer.impl.interfaces.PolymerNetworkHandlerExtension;
import eu.pb4.polymer.impl.client.ClientUtils;
import eu.pb4.polymer.mixin.block.packet.ThreadedAnvilChunkStorageAccessor;
Expand All @@ -12,10 +14,12 @@
import net.minecraft.network.packet.s2c.play.InventoryS2CPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.packettweaker.PacketContext;

import java.util.ArrayList;
import java.util.List;

/**
* General use case utils that can be useful in multiple situations
*/
Expand Down Expand Up @@ -98,7 +102,29 @@ public static TooltipContext getTooltipContext(@Nullable ServerPlayerEntity play
return player != null && player.networkHandler instanceof PolymerNetworkHandlerExtension h && h.polymer_advancedTooltip() ? TooltipContext.Default.ADVANCED : TooltipContext.Default.NORMAL;
}

public static Identifier id(String path) {
return new Identifier(ID, path);
public static List<PolymerItemGroup> getItemGroups(ServerPlayerEntity player) {
var list = new ArrayList<PolymerItemGroup>();

for (var group : InternalServerRegistry.ITEM_GROUPS) {
if (group.shouldSyncWithPolymerClient(player)) {
list.add(group);
}
}

var sync = new PolymerItemGroup.ItemGroupSyncer() {
@Override
public void send(PolymerItemGroup group) {
list.add(group);
}

@Override
public void remove(PolymerItemGroup group) {
list.remove(group);
}
};

PolymerItemGroup.LIST_EVENT.invoke((x) -> x.onItemGroupSync(player, sync));

return list;
}
}
49 changes: 47 additions & 2 deletions src/main/java/eu/pb4/polymer/impl/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,26 @@
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import eu.pb4.polymer.api.item.PolymerItemGroup;
import eu.pb4.polymer.api.item.PolymerItemUtils;
import eu.pb4.polymer.api.resourcepack.PolymerRPUtils;
import eu.pb4.polymer.api.utils.PolymerUtils;
import eu.pb4.polymer.impl.compat.CompatStatus;
import eu.pb4.polymer.impl.compat.polymc.PolyMcHelpers;
import eu.pb4.polymer.impl.interfaces.PolymerNetworkHandlerExtension;
import eu.pb4.polymer.impl.ui.CreativeTabListUi;
import eu.pb4.polymer.impl.ui.CreativeTabUi;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.command.CommandSource;
import net.minecraft.command.argument.IdentifierArgumentType;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.ApiStatus;

import java.nio.file.Path;
import java.util.Locale;

import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;
Expand All @@ -26,10 +34,28 @@ public class Commands {
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
var command = literal("polymer")
.requires((source) -> source.hasPermissionLevel(3))

.then(literal("generate")
.executes(Commands::generate));
.requires((source) -> source.hasPermissionLevel(3))
.executes(Commands::generate))
.then(literal("creative")
.then(argument("itemGroup", IdentifierArgumentType.identifier())
.suggests((context, builder) -> {
var remaining = builder.getRemaining().toLowerCase(Locale.ROOT);

var groups = PolymerUtils.getItemGroups(context.getSource().getPlayer());

CommandSource.forEachMatching(groups, remaining, PolymerItemGroup::getId, group -> {
builder.suggest(group.getId().toString(), group.getDisplayName());
});
return builder.buildFuture();
})
.executes(Commands::creativeTab)
)
.executes(Commands::creativeTab))
;

if (PolymerGlobalValues.DEVELOPER_MODE) {
if (PolymerImpl.DEVELOPER_MODE) {
command.then(literal("dev")
.then(literal("item-client")
.executes(Commands::itemClient))
Expand Down Expand Up @@ -66,6 +92,25 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
dispatcher.register(command);
}

private static int creativeTab(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
if (context.getSource().getPlayer().isCreative()) {
try {
var itemGroup = PolymerItemGroup.REGISTRY.get(context.getArgument("itemGroup", Identifier.class));
if (itemGroup != null && itemGroup.shouldSyncWithPolymerClient(context.getSource().getPlayer())) {
new CreativeTabUi(context.getSource().getPlayer(), itemGroup);
return 2;
}
} catch (Exception e) {
//
}

new CreativeTabListUi(context.getSource().getPlayer());
return 1;
} else {
return 0;
}
}

private static int itemClient(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var player = context.getSource().getPlayer();
var itemStack = player.getMainHandStack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.minecraft.util.Identifier;

public class InternalServerRegistry {
public static final Identifier POLYMER_ITEM_GROUP = PolymerUtils.id("items");
public static final Identifier POLYMER_ITEM_GROUP = PolymerImpl.id("items");

public static final ImplPolymerRegistry<PolymerItemGroup> ITEM_GROUPS = new ImplPolymerRegistry<>();
}
26 changes: 0 additions & 26 deletions src/main/java/eu/pb4/polymer/impl/PolymerGlobalValues.java

This file was deleted.

Loading

0 comments on commit 4ce613b

Please sign in to comment.