Skip to content

Commit

Permalink
Rename to Personal Time
Browse files Browse the repository at this point in the history
  • Loading branch information
Icohedron committed Jun 16, 2017
1 parent 8d7550e commit 85534c7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 39 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ repositories {
}

group 'io.github.icohedron'
version '1.0-PRERELEASE'
description = 'Allows clients/players to set their own time without affecting the server\'s'
version '1.0-PRERELEASE-1'
description = 'Allows players to set their own personal time of day'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rootProject.name = 'ClientTime'
rootProject.name = 'PersonalTime'

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.icohedron.clienttime;
package io.github.icohedron.personaltime;

import com.google.inject.Inject;
import eu.crushedpixel.sponge.packetgate.api.event.PacketEvent;
Expand All @@ -18,8 +18,6 @@
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.plugin.Dependency;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.permission.PermissionDescription;
import org.spongepowered.api.service.permission.PermissionService;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.World;
Expand All @@ -30,8 +28,8 @@
import java.util.Optional;
import java.util.UUID;

@Plugin(id = "clienttime", name = "Client Time", description = "Allows clients/players to set their own time without affecting the server\'s", version = "1.0-PRERELEASE", dependencies = @Dependency(id = "packetgate"))
public class ClientTime extends PacketListenerAdapter {
@Plugin(id = "personaltime", name = "Personal Time", description = "Allows players to set their own personal time of day", version = "1.0-PRERELEASE-1", dependencies = @Dependency(id = "packetgate"))
public class PersonalTime extends PacketListenerAdapter {

@Inject
private Logger logger;
Expand All @@ -46,16 +44,16 @@ public void onInitializationEvent(GameInitializationEvent event) {
PacketGate packetGate = packetGateOptional.get();
packetGate.registerListener(this, ListenerPriority.DEFAULT, SPacketTimeUpdate.class);
initializeCommands();
logger.info("Client Time has successfully initialized");
logger.info("Personal Time has successfully initialized");
} else {
logger.error("PacketGate is not installed. Client Time depends on PacketGate in order to work");
logger.error("PacketGate is not installed. Personal Time depends on PacketGate in order to work");
}
}

private void initializeCommands() {
CommandSpec clientTimeSetCommand = CommandSpec.builder()
.description(Text.of("Set client time"))
.permission("clienttime.command.set")
CommandSpec personalTimeSetCommand = CommandSpec.builder()
.description(Text.of("Set your personal time"))
.permission("personaltime.command.set")
.arguments(GenericArguments.onlyOne(GenericArguments.string(Text.of("time"))))
.executor((src, args) -> {
if (!(src instanceof Player)) {
Expand All @@ -68,10 +66,10 @@ private void initializeCommands() {
if (optionalTime.isPresent()) {
String time = optionalTime.get();
if (time.equalsIgnoreCase("day")) {
setClientTime(player, 1000);
setPersonalTime(player, 1000);
return CommandResult.success();
} else if (time.equalsIgnoreCase("night")) {
setClientTime(player, 14000);
setPersonalTime(player, 14000);
return CommandResult.success();
} else {
int intTime;
Expand All @@ -85,31 +83,31 @@ private void initializeCommands() {
sendMessage(player, "The number you have entered (" + time + ") is too small, it must be at least 0");
return CommandResult.empty();
}
setClientTime(player, intTime);
setPersonalTime(player, intTime);
return CommandResult.success();
}
}
return CommandResult.empty();
})
.build();

CommandSpec clientTimeResetCommand = CommandSpec.builder()
.description(Text.of("Reset client time"))
.permission("clienttime.command.reset")
CommandSpec personalTimeResetCommand = CommandSpec.builder()
.description(Text.of("Reset your personal time"))
.permission("personaltime.command.reset")
.executor((src, args) -> {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("This command may only be executed by a player"));
return CommandResult.empty();
}
Player player = (Player)src;
resetClientTime(player);
resetPersonalTime(player);
return CommandResult.success();
})
.build();

CommandSpec clientTimeStatusCommand = CommandSpec.builder()
.description(Text.of("Get the status of the client time"))
.permission("clienttime.command.status")
CommandSpec personalTimeStatusCommand = CommandSpec.builder()
.description(Text.of("Get the status of your personal time"))
.permission("personaltime.command.status")
.executor((src, args) -> {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("This command may only be executed by a player"));
Expand All @@ -127,19 +125,19 @@ private void initializeCommands() {
})
.build();

CommandSpec clientTimeCommand = CommandSpec.builder()
.description(Text.of("The one command for ClientTime"))
.permission("clienttime.command")
.child(clientTimeSetCommand, "set")
.child(clientTimeResetCommand, "reset")
.child(clientTimeStatusCommand, "status")
CommandSpec personalTimeCommand = CommandSpec.builder()
.description(Text.of("The one command for PersonalTime"))
.permission("personaltime.command")
.child(personalTimeSetCommand, "set")
.child(personalTimeResetCommand, "reset")
.child(personalTimeStatusCommand, "status")
.build();

Sponge.getCommandManager().register(this, clientTimeCommand, "clienttime", "ctime", "ptime");
Sponge.getCommandManager().register(this, personalTimeCommand, "personaltime", "ptime");
}

private void sendMessage(Player player, String text) {
player.sendMessage(Text.of(TextColors.GREEN, "[", TextColors.RED, "ClientTime", TextColors.GREEN, "] ", TextColors.YELLOW, text));
player.sendMessage(Text.of(TextColors.GREEN, "[", TextColors.RED, "PersonalTime", TextColors.GREEN, "] ", TextColors.YELLOW, text));
}

private String ticksToRealTime(long ticks) {
Expand All @@ -164,17 +162,17 @@ private String ticksToRealTime(long ticks) {
return hours + ":" + String.format("%02d", minutes) + " " + suffix;
}

private void setClientTime(Player player, long ticks) {
private void setPersonalTime(Player player, long ticks) {
World world = player.getWorld();
long worldTime = world.getProperties().getWorldTime();
long desiredClientTime = (long) Math.ceil(worldTime / 24000.0f) * 24000 + ticks; // Fast forward to the next '0' time and add the desired number of ticks
long timeOffset = desiredClientTime - worldTime;
long desiredPersonalTime = (long) Math.ceil(worldTime / 24000.0f) * 24000 + ticks; // Fast forward to the next '0' time and add the desired number of ticks
long timeOffset = desiredPersonalTime - worldTime;
timeOffsets.put(player.getUniqueId(), timeOffset);

sendMessage(player, "Set time to " + ticks + " (" + ticksToRealTime(ticks % 24000) + ")");
}

private void resetClientTime(Player player) {
private void resetPersonalTime(Player player) {
timeOffsets.put(player.getUniqueId(), 0L);
sendMessage(player, "Your time is now synchronized with the server's");
}
Expand All @@ -200,13 +198,13 @@ public void onPacketWrite(PacketEvent packetEvent, PacketConnection connection)
long totalWorldTime = packetBuffer.readLong();
long worldTime = packetBuffer.readLong();

long clientWorldTime;
long personalWorldTime;
if (worldTime < 0) {
clientWorldTime = worldTime - timeOffsets.get(playerUuid); // gamerule doDaylightCycle is false, which makes worldTime negative
personalWorldTime = worldTime - timeOffsets.get(playerUuid); // gamerule doDaylightCycle is false, which makes worldTime negative
} else {
clientWorldTime = worldTime + timeOffsets.get(playerUuid);
personalWorldTime = worldTime + timeOffsets.get(playerUuid);
}

packetEvent.setPacket(new SPacketTimeUpdate(totalWorldTime, clientWorldTime, true));
packetEvent.setPacket(new SPacketTimeUpdate(totalWorldTime, personalWorldTime, true));
}
}

0 comments on commit 85534c7

Please sign in to comment.