This commit is contained in:
2026-03-01 18:32:28 +01:00
parent 5a79447622
commit 0c9a6a7d64
76 changed files with 2444 additions and 193 deletions

View File

@@ -105,7 +105,8 @@ public class ClientCosmetics {
/* ---------------- FETCH MOJANG CAPES ---------------- */
public static void fetchMojangCapes(UUID uuid) {
try {MinecraftClient client = MinecraftClient.getInstance();
try {
MinecraftClient client = MinecraftClient.getInstance();
String accessToken = client.getSession().getAccessToken();
if (accessToken == null) return;
@@ -113,39 +114,49 @@ public class ClientCosmetics {
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization", "Bearer " + accessToken);
con.setRequestMethod("GET");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
InputStream in = con.getInputStream();
String json = new String(in.readAllBytes());
in.close();
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
JsonArray capes = obj.getAsJsonArray("capes");
if (capes == null) return;
List<MojangCape> list = new ArrayList<>();
for (JsonElement e : capes) {
JsonObject c = e.getAsJsonObject();
MojangCape cape = new MojangCape();
cape.id = c.get("id").getAsString();
cape.name = c.has("alias") ? c.get("alias").getAsString() : cape.id;
cape.url = c.get("url").getAsString();
list.add(cape);
int responseCode = con.getResponseCode();
if (responseCode != 200) {
// 401 or any other response → silently ignore
con.disconnect();
return;
}
PacketByteBuf buf = PacketByteBufs.create();
buf.writeUuid(uuid);
buf.writeInt(list.size());
try (InputStream in = con.getInputStream()) {
String json = new String(in.readAllBytes());
for (MojangCape cape : list) {
buf.writeString(cape.id);
buf.writeString(cape.name);
buf.writeString(cape.url);
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
JsonArray capes = obj.getAsJsonArray("capes");
if (capes == null) return;
List<MojangCape> list = new ArrayList<>();
for (JsonElement e : capes) {
JsonObject c = e.getAsJsonObject();
MojangCape cape = new MojangCape();
cape.id = c.get("id").getAsString();
cape.name = c.has("alias") ? c.get("alias").getAsString() : cape.id;
cape.url = c.get("url").getAsString();
list.add(cape);
}
PacketByteBuf buf = PacketByteBufs.create();
buf.writeUuid(uuid);
buf.writeInt(list.size());
for (MojangCape cape : list) {
buf.writeString(cape.id);
buf.writeString(cape.name);
buf.writeString(cape.url);
}
ClientPlayNetworking.send(MOJANG_CAPES_SYNC, buf);
}
ClientPlayNetworking.send(MOJANG_CAPES_SYNC, buf);
} catch (Exception ex) {
ex.printStackTrace();
// Only log unexpected exceptions
ex.printStackTrace(System.err);
}
}

View File

@@ -4,18 +4,26 @@ import com.mojang.blaze3d.systems.RenderSystem;
import dev.tggamesyt.szar.SlotMachineBlockEntity;
import dev.tggamesyt.szar.SlotMachineScreenHandler;
import dev.tggamesyt.szar.Szar;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.sound.PositionedSoundInstance;
import net.minecraft.client.sound.SoundInstance;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import dev.tggamesyt.szar.SlotSymbol;
import java.util.Objects;
public class SlotMachineScreen extends HandledScreen<SlotMachineScreenHandler> {
private static final Identifier BG_TEXTURE =
@@ -27,9 +35,15 @@ public class SlotMachineScreen extends HandledScreen<SlotMachineScreenHandler> {
new Identifier(Szar.MOD_ID, "textures/gui/handle2.png");
private static final Identifier HANDLE_3 =
new Identifier(Szar.MOD_ID, "textures/gui/handle3.png");
private PositionedSoundInstance spinSound;
private boolean wasSpinning = false;
private final int handleX = 120;
private final int handleY = 20;
// Track when to play win sound
private boolean winSoundPending = false;
private int winSoundDelay = 0; // ticks until sound plays
private boolean lastWinState = false; // tracks PropertyDelegate slot 1
private final PlayerInventory inventory;
private boolean handleClicked = false;
private int handleAnimTicks = 0;
@@ -41,6 +55,8 @@ public class SlotMachineScreen extends HandledScreen<SlotMachineScreenHandler> {
super(handler, inventory, title);
this.backgroundWidth = 176;
this.backgroundHeight = 166;
this.inventory = inventory;
inventory.player.playSound(Szar.LETS_GAMBLE, 1f, 1f);
}
// ----------------------------
@@ -85,14 +101,60 @@ public class SlotMachineScreen extends HandledScreen<SlotMachineScreenHandler> {
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
renderBackground(context);
super.render(context, mouseX, mouseY, delta);
if (client != null && client.player != null) {
handler.tick(client.player);
}
drawMouseoverTooltip(context, mouseX, mouseY);
boolean spinning = handler.getPropertyDelegate().get(0) == 1;
boolean won = handler.getPropertyDelegate().get(1) == 1;
// Start spin sound when spin starts
if (spinning && !wasSpinning) {
spinSound = new PositionedSoundInstance(
Szar.SLOT_MACHINE_BASE.getId(),
SoundCategory.MASTER,
5.0f,
1.0f,
net.minecraft.util.math.random.Random.create(),
true,
0,
SoundInstance.AttenuationType.NONE,
0.0, 0.0, 0.0,
true
);
MinecraftClient.getInstance().getSoundManager().play(spinSound);
}
// Stop spin sound when spin ends
if (!spinning && wasSpinning) {
stopSpinSound();
}
// === Delayed win sound logic ===
if (!lastWinState && spinning) {
// spin is in progress, first tick that win property is set → start delay
winSoundPending = true;
winSoundDelay = 0;
}
if (winSoundPending) {
winSoundDelay++;
if (winSoundDelay >= 15) { // 15-tick delay
if (inventory.player != null) {
if (won) {
inventory.player.playSound(Szar.SLOT_MACHINE_WIN, 5.0f, 1.0f);
inventory.player.playSound(Szar.WON, 5.0f, 1.0f);
} else {
inventory.player.playSound(Szar.DANGIT, 5.0f, 1.0f);
}
}
winSoundPending = false;
}
}
lastWinState = won;
wasSpinning = spinning;
int guiLeft = (width - backgroundWidth) / 2;
int guiTop = (height - backgroundHeight) / 2;
@@ -111,7 +173,7 @@ public class SlotMachineScreen extends HandledScreen<SlotMachineScreenHandler> {
// CALL SCREEN HANDLER LOGIC HERE
if (client != null && client.player != null && client.interactionManager != null) {
client.interactionManager.clickButton(handler.syncId, 0);
client.interactionManager.clickButton(handler.syncId, 0);
}
}
}
@@ -146,4 +208,16 @@ public class SlotMachineScreen extends HandledScreen<SlotMachineScreenHandler> {
return super.mouseClicked(mouseX, mouseY, button);
}
private void stopSpinSound() {
if (MinecraftClient.getInstance() != null && spinSound != null) {
MinecraftClient.getInstance().getSoundManager().stop(spinSound);
spinSound = null;
}
}
@Override
public void removed() {
super.removed();
stopSpinSound();
}
}

View File

@@ -118,7 +118,6 @@ public class SzarClient implements ClientModInitializer {
// Apply the cosmetic profile on the main thread
client.execute(() -> {
ClientCosmetics.fetchMojangCapes(playerUuid);
System.out.println("got it client");
ClientCosmetics.apply(playerUuid, nameType, staticColor, gradientStart, gradientEnd, capeTexture);
});
});