Skip to content

Commit

Permalink
Fixed username identifier bug
Browse files Browse the repository at this point in the history
Added some input validation
Cleanup
  • Loading branch information
PotatoPresident committed Jan 1, 2022
1 parent 2c31e0f commit 4041422
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ import net.quiltservertools.interdimensional.mixin.ChunkGeneratorSettingsAccesso
import net.quiltservertools.interdimensional.mixin.NoiseChunkGeneratorAccessor
import net.quiltservertools.interdimensional.text
import net.quiltservertools.interdimensional.world.RuntimeWorldManager
import org.apache.commons.lang3.RandomStringUtils
import xyz.nucleoid.fantasy.RuntimeWorldConfig
import xyz.nucleoid.fantasy.util.VoidChunkGenerator
import java.util.*

class CreateGuiHandler(val player: ServerPlayerEntity) {
val gui = SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false)
private val maplikeSelector = WorldSelectorElement(player.server.worlds, this)
class CreateGuiHandler(player: ServerPlayerEntity) : SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false) {
private val maplikeSelector = WorldSelectorElement(this.player.server.worlds, this)
private val biomeSourceSelector = BiomeSourceElement(this)

var maplike: ServerWorld = player.getWorld()
var type: GeneratorTypes = GeneratorTypes.NOISE
var biomeSource: BiomeSource = player.getWorld().chunkManager.chunkGenerator.biomeSource
var seed: Long = player.getWorld().seed
var identifier: Identifier = Identifier(player.gameProfile.name)
var maplike: ServerWorld = this.player.getWorld()
var genType: GeneratorTypes = GeneratorTypes.NOISE
var biomeSource: BiomeSource = this.player.getWorld().chunkManager.chunkGenerator.biomeSource
var seed: Long = this.player.getWorld().seed
var identifier: Identifier = Identifier(this.player.gameProfile.name.lowercase(), RandomStringUtils.randomNumeric(5))
var difficulty: Difficulty = Difficulty.NORMAL
var generatorSettings: ChunkGeneratorSettings =
BuiltinRegistries.CHUNK_GENERATOR_SETTINGS.get(ChunkGeneratorSettings.OVERWORLD)
Expand All @@ -45,19 +45,19 @@ class CreateGuiHandler(val player: ServerPlayerEntity) {
val generatorTypes = GeneratorTypes.values().toMutableList()

// World info
gui.addSlot(maplikeSelector.createElement())
addSlot(maplikeSelector.createElement())
// Identifier
gui.addSlot(TextComponent("Identifier", Items.WARPED_SIGN, this, IdentifierInputGui(this)))
addSlot(TextComponent("Identifier", Items.WARPED_SIGN, this, IdentifierInputGui(this, identifier)))
// Difficulty
DifficultySelectorElement(this, DifficultyOption.values().toMutableList())

// Generation
// Seed selector
gui.addSlot(TextComponent("Seed", Items.WHEAT_SEEDS, this, SeedInputGui(this)))
addSlot(TextComponent("Seed", Items.WHEAT_SEEDS, this, SeedInputGui(this)))
// Generator type selector
GeneratorTypeElement(this, generatorTypes)
// Biome source selector
gui.addSlot(biomeSourceSelector.createElement())
addSlot(biomeSourceSelector.createElement())
// Generator settings
ChunkGeneratorSettingsElement(this)

Expand All @@ -66,22 +66,18 @@ class CreateGuiHandler(val player: ServerPlayerEntity) {
StrongholdEnableComponent(this)

// Bottom row
gui.setSlot(18, ActionComponent(Items.LIME_CONCRETE, "Submit") { submit() })
gui.setSlot(26, ActionComponent(Items.RED_CONCRETE, "Close") { close() })
setSlot(18, ActionComponent(Items.LIME_CONCRETE, "Submit") { submit() })
setSlot(26, ActionComponent(Items.RED_CONCRETE, "Close") { close() })

gui.title = "Create".text()
title = "Create".text()
open()
}

fun open() {
gui.open()
}

@Suppress("CAST_NEVER_SUCCEEDS")
private fun submit() {
val biomeSource = biomeSourceSelector.result.biomeSource

val generator = when (type) {
val generator = when (genType) {
GeneratorTypes.NOISE -> {
if (structuresConfig.isPresent) {
(generatorSettings as ChunkGeneratorSettingsAccessor).setStructuresConfig(structuresConfig.get())
Expand Down Expand Up @@ -119,8 +115,4 @@ class CreateGuiHandler(val player: ServerPlayerEntity) {

close()
}

fun close() {
gui.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,19 @@ import net.quiltservertools.interdimensional.gui.elements.WorldDeleteElement
import net.quiltservertools.interdimensional.text
import net.quiltservertools.interdimensional.world.RuntimeWorldManager

class DeleteGuiHandler(val player: ServerPlayerEntity) {

val gui = SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false)
class DeleteGuiHandler(player: ServerPlayerEntity): SimpleGui(ScreenHandlerType.GENERIC_9X3, player, false) {
var identifier: Identifier? = null

init {
gui.addSlot(WorldDeleteElement(player.server.worlds, this).createElement())
addSlot(WorldDeleteElement(player.server.worlds, this).createElement())

// Bottom row
gui.setSlot(18, ActionComponent(Items.LIME_CONCRETE, "Submit") { submit() })
gui.setSlot(26, ActionComponent(Items.RED_CONCRETE, "Close") { close() })
setSlot(18, ActionComponent(Items.LIME_CONCRETE, "Submit") { submit() })
setSlot(26, ActionComponent(Items.RED_CONCRETE, "Close") { close() })

gui.title = "Delete".text()
title = "Delete".text()
open()
}
fun open() {
gui.open()
}
fun close() {
gui.close()
}

private fun submit() {
val identifier = this.identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ abstract class ShuffleComponent<T : Option>(val handler: CreateGuiHandler, val o
var index = 0

init {
handler.gui.addSlot(createElement(options.first()))
handler.addSlot(createElement(options.first()))
}

private fun showNext(slotIndex: Int) {
handler.gui.setSlot(slotIndex, createElement(options[index]))
handler.setSlot(slotIndex, createElement(options[index]))
}

private fun createElement(option: Option): GuiElementInterface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ class BiomeIdentifierInputGui(private val element: BiomeSourceElement) : TextCom
}

override fun close() {
element.result = SingleBiomeResult(element, element.handler.player.server.registryManager.get(Registry.BIOME_KEY).get(
Identifier(this.input)
)?: BuiltinBiomes.PLAINS)
super.close()
element.handler.open()
if (this.input.isNotEmpty() && Identifier.isValid(input)) {
element.result = SingleBiomeResult(
element, element.handler.player.server.registryManager.get(Registry.BIOME_KEY).get(
Identifier(this.input)
) ?: BuiltinBiomes.PLAINS
)
super.close()
element.handler.open()
} else {
this.open()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import net.minecraft.item.Items
import net.minecraft.screen.ScreenHandlerType
import net.minecraft.util.Identifier
import net.minecraft.util.registry.Registry
import net.minecraft.util.registry.RegistryKey
import net.minecraft.world.biome.Biome
import net.minecraft.world.biome.BiomeKeys
import net.minecraft.world.biome.source.MultiNoiseBiomeSource
import net.minecraft.world.biome.source.TheEndBiomeSource
import net.quiltservertools.interdimensional.gui.CreateGuiHandler
import net.quiltservertools.interdimensional.gui.biomeSource.BiomeSourceResult
import net.quiltservertools.interdimensional.gui.biomeSource.EndResult
import net.quiltservertools.interdimensional.gui.biomeSource.MultiNoiseResult
import net.quiltservertools.interdimensional.gui.biomeSource.SingleBiomeResult
import net.quiltservertools.interdimensional.gui.components.ActionComponent
import net.quiltservertools.interdimensional.gui.components.LinkComponent
import net.quiltservertools.interdimensional.text
Expand Down Expand Up @@ -64,6 +61,6 @@ class BiomeSourceElement(val handler: CreateGuiHandler) : LinkComponent {

override fun setResult(handler: CreateGuiHandler) {
handler.biomeSource = result.biomeSource
handler.gui.setSlot(this.handlerSlotIndex, this.createElement())
handler.setSlot(this.handlerSlotIndex, this.createElement())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import net.quiltservertools.interdimensional.gui.components.ShuffleComponent

class GeneratorTypeElement(handler: CreateGuiHandler, options: MutableList<GeneratorTypes>) : ShuffleComponent<GeneratorTypes>(handler, options) {
override fun setResult() {
handler.type = options[this.index]
handler.genType = options[this.index]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import net.quiltservertools.interdimensional.gui.CreateGuiHandler
import net.quiltservertools.interdimensional.gui.components.TextComponent
import net.quiltservertools.interdimensional.text

class IdentifierInputGui(val handler: CreateGuiHandler) : TextComponent.TextInputGui(handler.player) {
class IdentifierInputGui(val handler: CreateGuiHandler, current: Identifier) : TextComponent.TextInputGui(handler.player) {
init {
this.setDefaultInputValue(current.toString())
}

override fun onClose() {
if (this.input.isNotEmpty()) {
if (this.input.isNotEmpty() && Identifier.isValid(input)) {
handler.identifier = Identifier(this.input)
handler.open()
} else {
this.open()
}
handler.open()
}

override fun getItemStack(icon: Item, displayName: String): ItemStack {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import net.minecraft.item.ItemStack
import net.quiltservertools.interdimensional.gui.CreateGuiHandler
import net.quiltservertools.interdimensional.gui.components.TextComponent
import net.quiltservertools.interdimensional.text
import org.apache.commons.lang3.StringUtils

class SeedInputGui(val handler: CreateGuiHandler) : TextComponent.TextInputGui(handler.player) {
override fun getItemStack(icon: Item, displayName: String): ItemStack {
Expand All @@ -15,11 +16,29 @@ class SeedInputGui(val handler: CreateGuiHandler) : TextComponent.TextInputGui(h
}

override fun onClose() {
if (this.input.isNotEmpty()) {
handler.seed = this.input.toLong()
handler.seed = getSeed() ?: handler.player.getWorld().seed
handler.open()
}

private fun getSeed(): Long? {
val string: String = input
return if (StringUtils.isEmpty(string)) {
null
} else {
handler.seed = handler.player.getWorld().seed
val optionalLong2 = tryParseLong(string)
if (optionalLong2 != null && optionalLong2 != 0L) {
optionalLong2
} else {
string.hashCode().toLong()
}
}
}

private fun tryParseLong(string: String): Long? {
return try {
string.toLong()
} catch (e: NumberFormatException) {
null
}
handler.open()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class StructureSelectorElement(val handler: CreateGuiHandler) : LinkComponent {
}
gui.addSlot(ItemStack(Items.RED_CONCRETE).setCustomName("Disabled".text()), ComponentCallback(null, this))
setResult(handler)
handler.gui.addSlot(this.createElement())
handler.addSlot(this.createElement())
}

override fun getItemStack(): ItemStack {
Expand All @@ -46,7 +46,7 @@ class StructureSelectorElement(val handler: CreateGuiHandler) : LinkComponent {

override fun close() {
gui.close()
handler.gui.setSlot(this.handlerSlotIndex, this.createElement())
handler.setSlot(this.handlerSlotIndex, this.createElement())
handler.open()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class WorldDeleteElement(worlds: Iterable<ServerWorld>, val handler: DeleteGuiHa
GuiElementInterface.ClickCallback {
override fun click(index: Int, type: ClickType?, action: SlotActionType?, gui: SlotGuiInterface) {
element.handler.identifier = world.registryKey.value
element.handler.gui.setSlot(element.handlerSlotIndex, element.createElement())
element.handler.setSlot(element.handlerSlotIndex, element.createElement())
element.close()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import net.minecraft.item.Items
import net.minecraft.screen.ScreenHandlerType
import net.minecraft.screen.slot.SlotActionType
import net.minecraft.server.world.ServerWorld
import net.minecraft.util.registry.Registry
import net.quiltservertools.interdimensional.gui.CreateGuiHandler
import net.quiltservertools.interdimensional.gui.components.LinkComponent
import net.quiltservertools.interdimensional.text
import java.util.*

class WorldSelectorElement(worlds: Iterable<ServerWorld>, private val handler: CreateGuiHandler) :
LinkComponent {
Expand Down Expand Up @@ -43,7 +41,7 @@ class WorldSelectorElement(worlds: Iterable<ServerWorld>, private val handler: C

override fun setResult(handler: CreateGuiHandler) {
handler.maplike = this.result ?: handler.player.getWorld()
handler.gui.setSlot(handlerSlotIndex, this.createElement())
handler.setSlot(handlerSlotIndex, this.createElement())
}

override fun close() {
Expand Down

0 comments on commit 4041422

Please sign in to comment.