Skip to content

Commit

Permalink
it works almost - run client :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticpasta1 committed Dec 31, 2023
1 parent db8228f commit 9e73431
Show file tree
Hide file tree
Showing 20 changed files with 236 additions and 243 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ dependencies {
modImplementation "io.wispforest:worldmesher:${project.worldmesher_version}"
include "io.wispforest:worldmesher:${project.worldmesher_version}"

modImplementation include("io.github.cottonmc:LibGui:8.1.0+1.20.1")

implementation 'com.madgag:animated-gif-lib:1.4'
include 'com.madgag:animated-gif-lib:1.4'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,70 @@
import com.mystic.holographicrenders.blocks.projector.ItemProjectionHandler;
import com.mystic.holographicrenders.blocks.projector.ProjectorBlock;
import com.mystic.holographicrenders.blocks.projector.ProjectorBlockEntity;
import com.mystic.holographicrenders.client.TextboxScreenRoot;
import com.mystic.holographicrenders.gui.ProjectorScreenHandler;
import com.mystic.holographicrenders.item.AreaScannerItem;
import com.mystic.holographicrenders.item.EntityScannerItem;
import com.mystic.holographicrenders.item.TextureScannerItem;
import com.mystic.holographicrenders.item.WidgetScannerItem;
import com.mystic.holographicrenders.item.WidgetType;
import com.mystic.holographicrenders.network.ProjectorScreenPacket;
import io.github.cottonmc.cotton.gui.client.LibGui;
import io.github.cottonmc.cotton.gui.impl.LibGuiCommon;
import io.github.cottonmc.cotton.gui.impl.client.ItemUseChecker;
import io.github.cottonmc.cotton.gui.impl.client.LibGuiClient;
import io.netty.buffer.ByteBufUtil;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerType;
import net.fabricmc.fabric.api.screenhandler.v1.ScreenHandlerRegistry;
import net.fabricmc.fabric.impl.itemgroup.FabricItemGroup;
import net.fabricmc.fabric.impl.itemgroup.FabricItemGroupBuilderImpl;
import net.minecraft.block.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.inventory.StackReference;
import net.minecraft.item.*;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.Supplier;

public class HolographicRenders implements ModInitializer {

public static final String MOD_ID = "holographic_renders";

public static final ItemGroup HOLOGRAPHIC_RENDERS_CREATIVE_TAB = FabricItemGroup.create(new Identifier(MOD_ID, "general")).icon(() -> new ItemStack(HolographicRenders.PROJECTOR_BLOCK)).build().setName("holographic_renders:textures/gui/hologram_tab.png");
public static final List<Supplier<? extends ItemConvertible>> MAIN_BLOCKS = new ArrayList<>();
public static final ItemGroup owo = new FabricItemGroupBuilderImpl().icon(() -> HolographicRenders.PROJECTOR_BLOCK.asItem().getDefaultStack()).displayName(Text.literal("Holographic Renders")).entries((displayContext, entries) -> {
MAIN_BLOCKS.forEach((itemLike -> entries.add(itemLike.get())));
}).build();

public static ItemConvertible addToMainTab (ItemConvertible itemLike) {
MAIN_BLOCKS.add(() -> itemLike);
return itemLike;
}

public static final Item AREA_SCANNER = new AreaScannerItem();
public static final Item TEXTURE_SCANNER = new TextureScannerItem();
public static final Item ENTITY_SCANNER = new EntityScannerItem();
public static final Item WIDGET_SCANNER = new WidgetScannerItem();

public static final Block PROJECTOR_BLOCK = new ProjectorBlock();
public static final Item PROJECTOR_ITEM = new BlockItem(PROJECTOR_BLOCK, new Item.Settings().group(HOLOGRAPHIC_RENDERS_CREATIVE_TAB));
public static final Item PROJECTOR_ITEM = new BlockItem(PROJECTOR_BLOCK, new Item.Settings());
public static final BlockEntityType<ProjectorBlockEntity> PROJECTOR_BLOCK_ENTITY = FabricBlockEntityTypeBuilder.create(ProjectorBlockEntity::new, PROJECTOR_BLOCK).build(null);

public static final Identifier PROJECTOR_ID = new Identifier(MOD_ID, "projector");
Expand All @@ -59,6 +79,8 @@ public class HolographicRenders implements ModInitializer {

@Override
public void onInitialize() {
Properties props = System.getProperties();
props.setProperty("libgui.allowItemUse", "true");

Registry.register(Registries.BLOCK, PROJECTOR_ID, PROJECTOR_BLOCK);
Registry.register(Registries.ITEM, PROJECTOR_ID, PROJECTOR_ITEM);
Expand All @@ -69,12 +91,19 @@ public void onInitialize() {
Registry.register(Registries.ITEM, new Identifier(MOD_ID, "entity_scanner"), ENTITY_SCANNER);
Registry.register(Registries.ITEM, new Identifier(MOD_ID, "widget_scanner"), WIDGET_SCANNER);

addToMainTab(AREA_SCANNER.asItem());
addToMainTab(TEXTURE_SCANNER.asItem());
addToMainTab(ENTITY_SCANNER.asItem());
//addToMainTab(WIDGET_SCANNER.asItem());
addToMainTab(PROJECTOR_ITEM.asItem());

Registry.register(Registries.ITEM_GROUP, new Identifier(MOD_ID, "group"), owo);

ServerPlayNetworking.registerGlobalReceiver(ProjectorScreenPacket.ACTION_REQUEST_ID, ProjectorScreenPacket::onActionRequest);
ServerPlayNetworking.registerGlobalReceiver(new Identifier(HolographicRenders.MOD_ID, "url_packet"), (server, player, handler, buf, responseSender) -> {
String url = buf.readString();
Hand hand = buf.readEnumConstant(Hand.class);
ItemStack stack = player.getStackInHand(buf.readEnumConstant(Hand.class));
server.execute(() -> {
ItemStack stack = player.getStackInHand(hand);
if(stack.getItem() instanceof TextureScannerItem) {
NbtCompound tag = stack.getOrCreateNbt();
tag.putString("URL", url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public class ItemProjectionHandler {
}
});

registerBehaviour(stack -> stack.getItem() == HolographicRenders.WIDGET_SCANNER, (be, stack) -> {
try {
return RenderDataProvider.WidgetProvider.of(WidgetType.fromId(be.getStack(0).getOrCreateNbt().getInt("Widget")), be.getPos());
} catch (ExecutionException e) {
return RenderDataProvider.EmptyProvider.INSTANCE;
}
});
// registerBehaviour(stack -> stack.getItem() == HolographicRenders.WIDGET_SCANNER, (be, stack) -> {
// try {
// return RenderDataProvider.WidgetProvider.of(WidgetType.fromId(be.getStack(0).getOrCreateNbt().getInt("Widget")), be.getPos());
// } catch (ExecutionException e) {
// return RenderDataProvider.EmptyProvider.INSTANCE;
// }
// });

registerBehaviour(itemStack -> itemStack.getItem() == Items.FILLED_MAP, (be, stack) -> {
return RenderDataProvider.MapProvider.of(FilledMapItem.getMapId(stack));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry;
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry;

import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;

Expand All @@ -41,7 +39,7 @@ public void onInitializeClient() {
}
});
});
Common.textScreenRunnable = (hand -> MinecraftClient.getInstance().setScreen(new TextboxScreen(hand)));
Common.widgetScreenRunnable = (hand -> MinecraftClient.getInstance().setScreen(new WidgetScreen(hand)));
Common.textScreenRunnable = (hand -> MinecraftClient.getInstance().setScreen(new TextboxScreen(new TextboxScreenRoot(hand))));
Common.widgetScreenRunnable = (hand -> MinecraftClient.getInstance().setScreen(new WidgetScreen(new WidgetScreenRoot(hand), hand)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public static class TextureProvider extends RenderDataProvider<Identifier> {
.maximumSize(20)
.expireAfterAccess(20, TimeUnit.SECONDS)
.removalListener((RemovalListener<String, TextureProvider>) notification -> MinecraftClient.getInstance().getTextureManager().destroyTexture(notification.getValue().data))
.build(new CacheLoader<String, TextureProvider>() {
.build(new CacheLoader<>() {
@Override
public TextureProvider load(String key) {
try {
Expand All @@ -434,7 +434,7 @@ public TextureProvider load(String key) {

Identifier id = new Identifier("missingno");

return new TextureProvider(id, new RegularSprite(id, 16,16));
return new TextureProvider(id, new RegularSprite(id, 16, 16));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.mystic.holographicrenders.client;

import com.mystic.holographicrenders.HolographicRenders;
import io.github.cottonmc.cotton.gui.ItemSyncedGuiDescription;
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WButton;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import io.github.cottonmc.cotton.gui.widget.WLabel;
import io.github.cottonmc.cotton.gui.widget.WTextField;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.StackReference;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;


public class TextboxScreenRoot extends LightweightGuiDescription {

public TextboxScreenRoot(Hand hand) {
WGridPanel root = new WGridPanel();
WButton button = new WButton();
WTextField textFieldWidget = new WTextField(Text.of("Please enter a valid URL!"));
WLabel label = new WLabel(Text.literal("Save URL"));
setRootPanel(root);
root.setSize(256, 240);
textFieldWidget.setMaxLength(90);
root.add(textFieldWidget, 0, 5, 15, 10);
root.add(button, 4, 8, 6, 20);
root.add(label, 4, 8);
root.validate(this);
button.setOnClick(() -> {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeString(textFieldWidget.getText(), 2000);
buf.writeEnumConstant(hand);
ClientPlayNetworking.send(new Identifier(HolographicRenders.MOD_ID, "url_packet"), buf);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.mystic.holographicrenders.client;

import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mystic.holographicrenders.HolographicRenders;
import com.mystic.holographicrenders.mixin.VertexConsumerProviderImmediateAccessor;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import net.minecraft.client.render.*;
import net.minecraft.util.Identifier;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static net.minecraft.client.render.VertexFormats.*;
import static net.minecraft.client.render.VertexFormats.POSITION_TEXTURE;

public class TextureRenderLayer extends RenderLayer {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mystic.holographicrenders.client;

import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
import net.minecraft.util.Hand;

public class WidgetScreenRoot extends LightweightGuiDescription {

public WidgetScreenRoot(Hand hand) {
WGridPanel root = new WGridPanel();
root.validate(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,63 +1,80 @@
package com.mystic.holographicrenders.gui;

import static com.teamwizardry.librarianlib.core.util.Shorthand.vec;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mystic.holographicrenders.network.ProjectorScreenPacket;
import com.teamwizardry.librarianlib.facade.container.FacadeView;
import com.teamwizardry.librarianlib.facade.layers.SpriteLayer;
import com.teamwizardry.librarianlib.facade.layers.TextLayer;
import com.teamwizardry.librarianlib.facade.layers.text.TextFit;
import com.teamwizardry.librarianlib.facade.pastry.layers.PastryCheckbox;
import com.teamwizardry.librarianlib.facade.pastry.layers.PastryToggle;
import com.teamwizardry.librarianlib.mosaic.Mosaic;
import com.teamwizardry.librarianlib.mosaic.MosaicSprite;
import ll.dev.thecodewarrior.bitfont.typesetting.TextLayoutManager;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.gui.widget.CheckboxWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Language;

import java.util.function.Consumer;

public class ProjectorScreen extends FacadeView<ProjectorScreenHandler> {
private static final MosaicSprite TEXTURE = new Mosaic(new Identifier("holographic_renders", "textures/gui_hologram_projector.png"), 256,256).getSprite("main");
public class ProjectorScreen extends HandledScreen<ProjectorScreenHandler> {
private static final Identifier TEXTURE = new Identifier("holographic_renders", "textures/gui_hologram_projector.png");

private boolean lightsEnabled = false;

public ProjectorScreen(ProjectorScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, title);
getMain().setSize(vec(Textures.main.getWidth(), Textures.main.getHeight()));

SpriteLayer sprite = new SpriteLayer(Textures.main);
TextLayer text = new TextLayer(128, 7, Language.getInstance().get("block.holographic_renders.projector"));
text.fitToText(TextFit.BOTH);
text.setTextAlignment(TextLayoutManager.Alignment.LEFT);
sprite.add(text);

PastryCheckbox checkbox = new PastryCheckbox(110, 39, false);
checkbox.BUS.hook(PastryToggle.StateWillChangeEvent.class, event -> client.getNetworkHandler().sendPacket(ProjectorScreenPacket.createLightAction(event.getNewState())));
TextLayer light = new TextLayer(8, -1,"Light");
light.fitToText(TextFit.BOTH);
light.setTextAlignment(TextLayoutManager.Alignment.LEFT);
checkbox.add(light);
checkbox.setState(handler.getLight());

getMain().add(sprite);
getMain().add(checkbox);

getMain().onLayout(() -> {
text.setX(getMain().getWidth() * 0.5 - text.getWidth() * 0.5);
}

@Override
protected void drawBackground(DrawContext matrices, float delta, int mouseX, int mouseY) {
RenderSystem.setShaderFogColor(1.0F, 1.0F, 1.0F, 1.0F);
client.getTextureManager().bindTexture(TEXTURE);
int x = (width - backgroundWidth) / 2;
int y = (height - backgroundHeight) / 2;
matrices.drawTexture(TEXTURE, x, y, 0, 0, backgroundWidth, backgroundHeight);
}

@Override
public void render(DrawContext matrices, int mouseX, int mouseY, float delta) {
renderBackground(matrices);
super.render(matrices, mouseX, mouseY, delta);
drawMouseoverTooltip(matrices, mouseX, mouseY);
}

@Override
protected void init() {
super.init();
// Center the title
titleX = (backgroundWidth - textRenderer.getWidth(title)) / 2;

int x = (width - backgroundWidth) / 2;
int y = (height - backgroundHeight) / 2;

CheckboxWidget lightCheckbox = new CallbackCheckboxWidget(x + 110, y + 33, Text.of("Light"), lightsEnabled, aBoolean -> {
client.getNetworkHandler().sendPacket(ProjectorScreenPacket.createLightAction(aBoolean));
});
addDrawable(lightCheckbox);

}

public void setLights(boolean lightsEnabled) {
this.lightsEnabled = lightsEnabled;
reload();
}

private void reload() {
this.init(MinecraftClient.getInstance(), this.width, this.height);
}

protected static class CallbackCheckboxWidget extends CheckboxWidget {

private final Consumer<Boolean> changeCallback;

public CallbackCheckboxWidget(int x, int y, Text message, boolean checked, Consumer<Boolean> changeCallback) {
super(x, y, 20, 20, message, checked);
this.changeCallback = changeCallback;
}

@Override
public void onPress() {
super.onPress();
changeCallback.accept(isChecked());
}
}
}
}
Loading

0 comments on commit 9e73431

Please sign in to comment.