Skip to content

Commit

Permalink
Fixin my math
Browse files Browse the repository at this point in the history
  • Loading branch information
samolego committed Sep 18, 2021
1 parent b2197db commit ab73dd4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public interface TailoredPlayer {
/**
* Gets player's skin value.
*
* @return skin value as string
* @return skin value as string, null if player has no skin set.
*/
@Nullable
String getSkinValue();

/**
* Gets player's skin signature.
*
* @return skin signature as string
* @return skin signature as string, null if player has no skin set.
*/
@Nullable
String getSkinSignature();
Expand All @@ -52,6 +52,11 @@ public interface TailoredPlayer {
*/
long getLastSkinChange();

/**
* Resets the skin timer.
*/
void resetLastSkinChange();

/**
* Clears player's skin.
*/
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/org/samo_lego/fabrictailor/command/SkinCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.util.Formatting;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -151,7 +152,7 @@ private static boolean setSkin(ServerPlayerEntity player, @NotNull Property skin
long lastChange = ((TailoredPlayer) player).getLastSkinChange();
long now = System.currentTimeMillis();

if(now - lastChange > config.skinChangeTimer * 1000) {
if(now - lastChange > config.skinChangeTimer * 1000 || lastChange == 0) {

if(!TATERZENS_LOADED || !TaterzensCompatibility.setTaterzenSkin(player, skinData)) {
((TailoredPlayer) player).setSkin(skinData, true);
Expand All @@ -162,8 +163,11 @@ private static boolean setSkin(ServerPlayerEntity player, @NotNull Property skin

} else {
// Prevent skin change spamming
MutableText timeLeft = new LiteralText(String.valueOf((config.skinChangeTimer * 1000 - now + lastChange) / 1000))
.formatted(Formatting.LIGHT_PURPLE);
player.sendMessage(
new TranslatedText("command.fabrictailor.skin.timer.please_wait", now - config.skinChangeTimer * 1000 - lastChange),
new TranslatedText("command.fabrictailor.skin.timer.please_wait", timeLeft)
.formatted(Formatting.RED),
false
);
}
Expand All @@ -176,7 +180,7 @@ public static boolean clearSkin(ServerPlayerEntity player) {
long lastChange = ((TailoredPlayer) player).getLastSkinChange();
long now = System.currentTimeMillis();

if(now - lastChange > config.skinChangeTimer * 1000) {
if(now - lastChange > config.skinChangeTimer * 1000 || lastChange == 0) {
((TailoredPlayer) player).clearSkin();
player.sendMessage(
new TranslatedText("command.fabrictailor.skin.clear.success").formatted(Formatting.GREEN),
Expand All @@ -186,10 +190,14 @@ public static boolean clearSkin(ServerPlayerEntity player) {
}

// Prevent skin change spamming
MutableText timeLeft = new LiteralText(String.valueOf((config.skinChangeTimer * 1000 - now + lastChange) / 1000))
.formatted(Formatting.LIGHT_PURPLE);
player.sendMessage(
new TranslatedText("command.fabrictailor.skin.timer.please_wait", now - config.skinChangeTimer * 1000 - lastChange),
new TranslatedText("command.fabrictailor.skin.timer.please_wait", timeLeft)
.formatted(Formatting.RED),
false
);
;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ private void onPlayerConnect(ClientConnection clientConnection, ServerPlayerEnti
String value = ((TailoredPlayer) player).getSkinValue();
String signature = ((TailoredPlayer) player).getSkinSignature();

Property skinData = null;
if(value == null || signature == null) {

Property skinData = null;

if(!config.defaultSkin.applyToAll)
skinData = fetchSkinByName(player.getGameProfile().getName());

Expand All @@ -43,9 +42,13 @@ private void onPlayerConnect(ClientConnection clientConnection, ServerPlayerEnti
skinData = new Property("textures", value, signature);
}

// Try to set skin now
if(skinData != null)
((TailoredPlayer) player).setSkin(skinData, false);

} else {
skinData = new Property("textures", value, signature);
}
// Try to set skin now
if(skinData != null)
((TailoredPlayer) player).setSkin(skinData, false);
((TailoredPlayer) player).resetLastSkinChange();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import net.minecraft.network.packet.c2s.play.CustomPayloadC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.LiteralText;
import net.minecraft.text.MutableText;
import net.minecraft.util.Formatting;
import org.samo_lego.fabrictailor.casts.TailoredPlayer;
import org.samo_lego.fabrictailor.mixin.accessors.ClientSettingsC2SAccessor;
import org.samo_lego.fabrictailor.util.TranslatedText;
Expand All @@ -28,7 +31,7 @@ private void onSkinChangePacket(CustomPayloadC2SPacket packet, CallbackInfo ci)
long lastChange = ((TailoredPlayer) this.player).getLastSkinChange();
long now = System.currentTimeMillis();
if(packet.getChannel().equals(FABRICTAILOR_CHANNEL)) {
if(now - lastChange > config.skinChangeTimer * 1000) {
if(now - lastChange > config.skinChangeTimer * 1000 || lastChange == 0) {
// This is our skin change packet
PacketByteBuf buf = packet.getData();
String value = buf.readString();
Expand All @@ -37,8 +40,11 @@ private void onSkinChangePacket(CustomPayloadC2SPacket packet, CallbackInfo ci)
((TailoredPlayer) this.player).setSkin(value, signature, true);
} else {
// Prevent skin change spamming
MutableText timeLeft = new LiteralText(String.valueOf((config.skinChangeTimer * 1000 - now + lastChange) / 1000))
.formatted(Formatting.LIGHT_PURPLE);
player.sendMessage(
new TranslatedText("command.fabrictailor.skin.timer.please_wait", now - config.skinChangeTimer * 1000 - lastChange),
new TranslatedText("command.fabrictailor.skin.timer.please_wait", timeLeft)
.formatted(Formatting.RED),
false
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ public class ServerPlayerEntityMixin_TailoredPlayer implements TailoredPlayer {
private final ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
private final GameProfile gameProfile = player.getGameProfile();

@Unique
private String skinValue;
@Unique
private String skinSignature;
@Unique
private final PropertyMap map = this.gameProfile.getProperties();
@Unique
private long lastSkinChangeTime;
private long lastSkinChangeTime = 0;


/**
Expand Down Expand Up @@ -157,7 +153,11 @@ public void clearSkin() {
} catch (Exception ignored) {
// Player has no skin data, no worries
}
}

@Override
public void resetLastSkinChange() {
this.lastSkinChangeTime = 0;
}


Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/data/fabrictailor/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"command.fabrictailor.skin.clear.success": "Skin was cleared successfully.",
"command.fabrictailor.skin.clear.error": "An error occurred when trying to clear your skin.",
"command.fabrictailor.skin.please_wait": "Uploading skin. Please wait.",
"command.fabrictailor.skin.timer.please_wait": "You've changed your skin recently. You must wait for %s seconds to change it again.",
"command.fabrictailor.skin.timer.please_wait": "You've changed your skin recently. You must wait %s seconds to change it again.",
"command.fabrictailor.skin.upload.failed": "A problem occurred when trying to upload the skin.",
"command.fabrictailor.skin.upload.malformed_url": "Malformed url!",

Expand Down

0 comments on commit ab73dd4

Please sign in to comment.