container API tweaks

This commit is contained in:
Adrian Siekierka 2018-12-22 18:46:42 +01:00
parent 5b71b9c946
commit 1f5a81e03e
6 changed files with 46 additions and 22 deletions

View file

@ -14,7 +14,7 @@
* limitations under the License.
*/
package net.fabricmc.fabric.api.container;
package net.fabricmc.fabric.api.client.gui;
import net.minecraft.client.gui.ContainerGui;
import net.minecraft.container.Container;

View file

@ -18,7 +18,6 @@ package net.fabricmc.fabric.api.client.gui;
import net.fabricmc.fabric.api.container.ContainerFactory;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry;
import net.fabricmc.fabric.api.container.GuiFactory;
import net.fabricmc.fabric.impl.client.gui.GuiProviderImpl;
import net.minecraft.client.gui.ContainerGui;
import net.minecraft.container.Container;
@ -30,20 +29,21 @@ public interface GuiProviderRegistry {
/**
*
* Register a gui factory, this should only be done on the client side and not on the dedicated server.
*
* @param identifier a shared identifier, this identifier should also be used to register a container using {@link ContainerProviderRegistry}
* @param factory the gui factory, this should return a new {@link ContainerGui}
*/
void registerFactory(Identifier identifier, ContainerFactory<ContainerGui> factory);
/**
*
* Register a GuiFactory that will be used to create a new gui when provided with a container
* Register a "Container -> ContainerGui" factory. This is used only on the client side.
*
* @param identifier a shared identifier, this identifier should also be used to register a container using {@link ContainerProviderRegistry}
* @param guiFactory the supplier that should be used to create the new gui
*/
<C extends Container> void registerFactory(Identifier identifier, GuiFactory<C> guiFactory);
/**
*
* Register a "packet -> ContainerGui" factory. This is used only on the client side, and allows you
* to override the default behaviour of re-using the existing "packet -> Container" logic.
*
* @param identifier a shared identifier, this identifier should also be used to register a container using {@link ContainerProviderRegistry}
* @param factory the gui factory, this should return a new {@link ContainerGui}
*/
void registerFactory(Identifier identifier, ContainerFactory<ContainerGui> factory);
}

View file

@ -25,7 +25,7 @@ public interface ContainerFactory<T> {
/**
*
* Creates the new gui or container
* Creates the new object.
*
* @param identifier the Identifier is the name that was used when registering the factory
* @param player the player that is opening the gui/container

View file

@ -19,6 +19,7 @@ package net.fabricmc.fabric.api.container;
import net.fabricmc.fabric.api.client.gui.GuiProviderRegistry;
import net.fabricmc.fabric.impl.container.ContainerProviderImpl;
import net.minecraft.container.Container;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.PacketByteBuf;
@ -31,7 +32,7 @@ public interface ContainerProviderRegistry {
/**
*
* Register a container factory
* Register a "packet buffer -> container" factory. This is used both on the client and server side.
*
* @param identifier a shared identifier, this identifier should also be used to register a container using {@link GuiProviderRegistry}
* @param factory the ContainerFactory that should return a new {@link Container}
@ -40,7 +41,7 @@ public interface ContainerProviderRegistry {
/**
*
* This is used to open a container on the client, call this from the server
* Open a modded container.
*
* @param identifier the identifier that was used when registering the container
* @param player the player that should open the container
@ -48,4 +49,17 @@ public interface ContainerProviderRegistry {
*/
void openContainer(Identifier identifier, ServerPlayerEntity player, Consumer<PacketByteBuf> writer);
/**
*
* Open a modded container. This should be called on the server side - it has no effect on the client side.
*
* @param identifier the identifier that was used when registering the container
* @param player the player that should open the container
* @param writer a PacketByteBuf where data can be written to, this data is then accessible by the container factory when creating the container or the gui
*/
default void openContainer(Identifier identifier, PlayerEntity player, Consumer<PacketByteBuf> writer) {
if (player instanceof ServerPlayerEntity) {
openContainer(identifier, (ServerPlayerEntity) player, writer);
}
}
}

View file

@ -19,7 +19,7 @@ package net.fabricmc.fabric.impl.client.gui;
import net.fabricmc.fabric.api.client.gui.GuiProviderRegistry;
import net.fabricmc.fabric.api.container.ContainerFactory;
import net.fabricmc.fabric.api.container.ContainerProviderRegistry;
import net.fabricmc.fabric.api.container.GuiFactory;
import net.fabricmc.fabric.api.client.gui.GuiFactory;
import net.fabricmc.fabric.impl.container.ContainerProviderImpl;
import net.fabricmc.fabric.networking.CustomPayloadPacketRegistry;
import net.minecraft.client.MinecraftClient;
@ -46,7 +46,7 @@ public class GuiProviderImpl implements GuiProviderRegistry {
public void registerFactory(Identifier identifier, ContainerFactory<ContainerGui> factory) {
if (FACTORIES.containsKey(identifier)) {
throw new RuntimeException("A factory has already been registered as " + identifier.toString());
throw new RuntimeException("A factory has already been registered as " + identifier + "!");
}
FACTORIES.put(identifier, factory);
}
@ -54,9 +54,9 @@ public class GuiProviderImpl implements GuiProviderRegistry {
@Override
public <C extends Container> void registerFactory(Identifier identifier, GuiFactory<C> guiFactory) {
registerFactory(identifier, (identifier1, player, buf) -> {
C container = ((ContainerProviderImpl)ContainerProviderRegistry.INSTANCE).createContainer(identifier1, player, buf);
C container = ContainerProviderImpl.INSTANCE.createContainer(identifier1, player, buf);
if(container == null){
LOGGER.error("A null container was created for " + identifier1.toString());
LOGGER.error("Could not open container for %s - a null object was created!", identifier1.toString());
return null;
}
return guiFactory.create(container);
@ -70,7 +70,7 @@ public class GuiProviderImpl implements GuiProviderRegistry {
MinecraftClient.getInstance().execute(() -> {
ContainerFactory<ContainerGui> factory = FACTORIES.get(identifier);
if (factory == null) {
LOGGER.error("No factory found for " + identifier.toString());
LOGGER.error("No GUI factory found for %s!", identifier.toString());
return;
}
ContainerGui gui = factory.create(identifier, packetContext.getPlayer(), packetByteBuf);

View file

@ -37,7 +37,7 @@ public class ContainerProviderImpl implements ContainerProviderRegistry {
/**
* Use the instance provided by ContainerProviderRegistry
*/
public static final ContainerProviderRegistry INSTANCE = new ContainerProviderImpl();
public static final ContainerProviderImpl INSTANCE = new ContainerProviderImpl();
private static final Logger LOGGER = LogManager.getLogger();
@ -52,6 +52,16 @@ public class ContainerProviderImpl implements ContainerProviderRegistry {
FACTORIES.put(identifier, factory);
}
@Override
public void openContainer(Identifier identifier, PlayerEntity player, Consumer<PacketByteBuf> writer) {
if (!(player instanceof ServerPlayerEntity)) {
LOGGER.warn("Please only use ContainerProviderRegistry.openContainer() with server-sided player entities!");
return;
}
openContainer(identifier, player, writer);
}
@Override
public void openContainer(Identifier identifier, ServerPlayerEntity player, Consumer<PacketByteBuf> writer) {
SyncIdProvider syncIDProvider = (SyncIdProvider) player;
@ -79,7 +89,7 @@ public class ContainerProviderImpl implements ContainerProviderRegistry {
public <C extends Container> C createContainer(Identifier identifier, PlayerEntity player, PacketByteBuf buf){
ContainerFactory<Container> factory = FACTORIES.get(identifier);
if (factory == null) {
LOGGER.error("No container factory found for %s ", identifier.toString());
LOGGER.error("No container factory found for %s!", identifier.toString());
return null;
}
return (C) factory.create(identifier, player, buf);