Skip to content
This repository has been archived by the owner on Oct 16, 2022. It is now read-only.

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederoxDev committed Aug 30, 2022
1 parent 6cdff33 commit 3ac389c
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 128 deletions.
256 changes: 129 additions & 127 deletions BP/scripts/Oxygen.ts
Original file line number Diff line number Diff line change
@@ -1,169 +1,171 @@
// import { Player, world, EntityInventoryComponent, Location } from "mojang-minecraft"
// import { Data, Debug } from "./Utility"
// import { PlayerData } from "./Classes/PlayerData"
// import { BaseBlocks } from "./Classes/BaseBlocks";
import { Player, world, EntityInventoryComponent, Location } from "mojang-minecraft"
import { Data, Debug } from "./Utility"
import { PlayerData } from "./Classes/PlayerData"
import { BaseBlocks } from "./Classes/BaseBlocks";

// const tankCapacity = 120
const tankCapacity = 120

// world.events.beforeItemUseOn.subscribe(e => {
// const location = e.blockLocation;
// const overworld = world.getDimension("overworld");
world.events.beforeItemUseOn.subscribe(e => {
const location = e.blockLocation;
const overworld = world.getDimension("overworld");

// if (overworld.getBlock(location).id != "cosmos:oxygen_charger") return;
// if (e.item == undefined || e.item.id != "cosmos:oxygen_tank") return;
if (overworld.getBlock(location).id != "cosmos:oxygen_charger") return;
if (e.item == undefined || e.item.id != "cosmos:oxygen_tank") return;

// const item = e.item
// item.setLore([`Oxygen: ${tankCapacity} / ${tankCapacity}`])
// item.getComponent("durability").damage = 1
const item = e.item
item.setLore([`Oxygen: ${tankCapacity} / ${tankCapacity}`])
item.getComponent("durability").damage = 1

// const player = <Player>e.source;
// const inventory = (<EntityInventoryComponent>player.getComponent("inventory")).container
const player = <Player>e.source;
const inventory = (<EntityInventoryComponent>player.getComponent("inventory")).container

// inventory.setItem(player.selectedSlot, item)
// player.playSound("air_fill")
// })
inventory.setItem(player.selectedSlot, item)
player.playSound("air_fill")
})

// var baseCooldown = new Date().getTime()
var baseCooldown = new Date().getTime()

// world.events.beforeItemUseOn.subscribe(e => {
// const location = e.blockLocation;
// const overworld = world.getDimension("overworld");
world.events.beforeItemUseOn.subscribe(e => {
const location = e.blockLocation;
const overworld = world.getDimension("overworld");

// if (overworld.getBlock(location).id != "cosmos:base_controller") return;
// if (new Date().getTime() - baseCooldown < 5) return;
if (overworld.getBlock(location).id != "cosmos:base_controller") return;
if (new Date().getTime() - baseCooldown < 5) return;

// //@ts-ignore
// (<Player>e.source).playSound("fall.amethyst_block", { volume: 12 })
//@ts-ignore
(<Player>e.source).playSound("fall.amethyst_block", { volume: 12 })

// //@ts-ignore
// const baseEntity = Array.from(world.getDimension("overworld").getEntities({
// location: e.source.location,
// type: "cosmos:base_bounds",
// closest: 1
// }))[0]
//@ts-ignore
const baseEntity = Array.from(world.getDimension("overworld").getEntities({
location: e.source.location,
type: "cosmos:base_bounds",
closest: 1
}))[0]

// if (baseEntity != undefined) {
// baseEntity.teleport(new Location(0, 320, 0), world.getDimension("overworld"), 0, 0)
// baseEntity.kill()
// return;
// }
if (baseEntity != undefined) {
baseEntity.teleport(new Location(0, 320, 0), world.getDimension("overworld"), 0, 0)
baseEntity.kill()
return;
}

// baseCooldown = new Date().getTime()
// world.getDimension("overworld").spawnEntity("cosmos:base_bounds", location)
// })
baseCooldown = new Date().getTime()
world.getDimension("overworld").spawnEntity("cosmos:base_bounds", location)
})

// world.events.blockPlace.subscribe(e => {
// if (e.block.id != "cosmos:base_controller") return;
// const location = e.block.location;
// var baseBlocks: BaseBlocks[] = JSON.parse(Data.GetKey("baseBlocks") || "[]")
world.events.blockPlace.subscribe(e => {
if (e.block.id != "cosmos:base_controller") return;
const location = e.block.location;
var baseBlocks: BaseBlocks[] = JSON.parse(Data.GetKey("baseBlocks") || "[]")

// var index = baseBlocks.findIndex(block => block.x == location.x && block.y == location.y && block.z == location.z)
var index = baseBlocks.findIndex(block => block.x == location.x && block.y == location.y && block.z == location.z)

// if (index == -1) {
// baseBlocks.push({ x: location.x, y: location.y, z: location.z })
// }
// else baseBlocks[index] = { x: location.x, y: location.y, z: location.z }
if (index == -1) {
baseBlocks.push({ x: location.x, y: location.y, z: location.z })
}
else baseBlocks[index] = { x: location.x, y: location.y, z: location.z }

// e.player.runCommand("tellraw @s {\"rawtext\":[{\"translate\":\"block.tips:base_controller\"}]}")
e.player.runCommand("tellraw @s {\"rawtext\":[{\"translate\":\"block.tips:base_controller\"}]}")

// Data.SetKey("baseBlocks", JSON.stringify(baseBlocks))
// })
Data.SetKey("baseBlocks", JSON.stringify(baseBlocks))
})

// world.events.blockBreak.subscribe(e => {
// if (e.brokenBlockPermutation.type.id != "cosmos:base_controller") return;
// const location = e.block.location;
world.events.blockBreak.subscribe(e => {
if (e.brokenBlockPermutation.type.id != "cosmos:base_controller") return;
const location = e.block.location;

// var baseBlocks: BaseBlocks[] = JSON.parse(Data.GetKey("baseBlocks") || "[]")
// var index = baseBlocks.findIndex(block => block.x == location.x && block.y == location.y && block.z == location.z)
var baseBlocks: BaseBlocks[] = JSON.parse(Data.GetKey("baseBlocks") || "[]")
var index = baseBlocks.findIndex(block => block.x == location.x && block.y == location.y && block.z == location.z)

// if (baseBlocks.length == 1) baseBlocks = []
// else baseBlocks = baseBlocks.slice(index)
if (baseBlocks.length == 1) baseBlocks = []
else baseBlocks = baseBlocks.slice(index)

// const baseEntity = Array.from(world.getDimension("overworld").getEntities({
// //@ts-ignore
// location: e.block.location,
// type: "cosmos:base_bounds",
// closest: 1
// }))[0]
const baseEntity = Array.from(world.getDimension("overworld").getEntities({
//@ts-ignore
location: e.block.location,
type: "cosmos:base_bounds",
closest: 1
}))[0]

// if (baseEntity != undefined) {
// baseEntity.teleport(new Location(0, 320, 0), world.getDimension("overworld"), 0, 0)
// baseEntity.kill()
// return;
// }
if (baseEntity != undefined) {
baseEntity.teleport(new Location(0, 320, 0), world.getDimension("overworld"), 0, 0)
baseEntity.kill()
return;
}

// Data.SetKey("baseBlocks", JSON.stringify(baseBlocks))
// })
Data.SetKey("baseBlocks", JSON.stringify(baseBlocks))
})

// const timeBetweenChecks = 1
// var counter = 0
const timeBetweenChecks = 1
var counter = 0

// world.events.tick.subscribe(e => {
// counter += e.deltaTime
// if (counter < timeBetweenChecks) return;
// else counter = 0
world.events.tick.subscribe(e => {
counter += e.deltaTime
if (counter < timeBetweenChecks) return;
else counter = 0

// const playerData: PlayerData[] = JSON.parse(Data.GetKey("playerData"))
// const players = Array.from(world.getPlayers());
// const baseBlocks = JSON.parse(Data.GetKey("baseBlocks"))
const playerData: PlayerData[] = JSON.parse(Data.GetKey("playerData") || "[]")
const players = Array.from(world.getPlayers());
const baseBlocks = JSON.parse(Data.GetKey("baseBlocks") || "[]")

// for (var i = 0; i < players.length; i++) {
// const player = players[i]
// const index = playerData.findIndex(playerData => playerData.name == player.name)
for (var i = 0; i < players.length; i++) {
const player = players[i]
const index = playerData.findIndex(playerData => playerData.name == player.name)

// if (playerData[index].planet === "Earth") return
if (index == -1) continue;

// var inBase = false
if (playerData[index].planet === "Earth") return

// for (var b = 0; b < baseBlocks.length; b++) {
// const location = new Location(baseBlocks[b].x, baseBlocks[b].y, baseBlocks[b].z)
// if (location.isNear(player.location, 5)) {
// player.onScreenDisplay.setActionBar("In Base")
// inBase = true
// break
// }
// }
var inBase = false

// if (inBase) continue;
for (var b = 0; b < baseBlocks.length; b++) {
const location = new Location(baseBlocks[b].x, baseBlocks[b].y, baseBlocks[b].z)
if (location.isNear(player.location, 5)) {
player.onScreenDisplay.setActionBar("In Base")
inBase = true
break
}
}

// const inventory = (<EntityInventoryComponent>player.getComponent("inventory")).container
// var hasOxygen = false;
if (inBase) continue;

// var maxOxygen = 0
// var currentOxygen = 0
const inventory = (<EntityInventoryComponent>player.getComponent("inventory")).container
var hasOxygen = false;

// for (var i = 0; i < inventory.size; i++) {
// const item = inventory.getItem(i)
// if (item == null || item.id != "cosmos:oxygen_tank") continue

// maxOxygen += tankCapacity;
var maxOxygen = 0
var currentOxygen = 0

// if (item.getLore()[0] == undefined) {
// item.setLore([`Oxygen: 0 / ${tankCapacity}`])
// inventory.setItem(i, item)
// continue
// }
for (var i = 0; i < inventory.size; i++) {
const item = inventory.getItem(i)
if (item == null || item.id != "cosmos:oxygen_tank") continue

// var durability = parseInt(item.getLore()[0].replace("Oxygen: ", "").split(" / ")[0])
// if (durability == 0) continue;
maxOxygen += tankCapacity;

// currentOxygen += durability
// if (hasOxygen) continue;
if (item.getLore()[0] == undefined) {
item.setLore([`Oxygen: 0 / ${tankCapacity}`])
inventory.setItem(i, item)
continue
}

// hasOxygen = true
// item.setLore([`Oxygen: ${durability - 1} / ${tankCapacity}`])
// item.getComponent("durability").damage = tankCapacity + 1 - durability
var durability = parseInt(item.getLore()[0].replace("Oxygen: ", "").split(" / ")[0])
if (durability == 0) continue;

// currentOxygen -= 1
// inventory.setItem(i, item)
// }
currentOxygen += durability
if (hasOxygen) continue;

// if (hasOxygen) {
// player.onScreenDisplay.setActionBar(`Oxygen: ${currentOxygen} / ${maxOxygen}`)
// continue
// }
hasOxygen = true
item.setLore([`Oxygen: ${durability - 1} / ${tankCapacity}`])
item.getComponent("durability").damage = tankCapacity + 1 - durability

// player.onScreenDisplay.setActionBar("No Oxygen!")
// player.runCommand("damage @s 2")
// }
// })
currentOxygen -= 1
inventory.setItem(i, item)
}

if (hasOxygen) {
player.onScreenDisplay.setActionBar(`Oxygen: ${currentOxygen} / ${maxOxygen}`)
continue
}

player.onScreenDisplay.setActionBar("No Oxygen!")
player.runCommand("damage @s 2")
}
})
2 changes: 1 addition & 1 deletion BP/scripts/PlayerWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Data } from "./Utility"

// If the player has not joined before, setup default playerData
world.events.playerJoin.subscribe(e => {
var playerData = JSON.parse(Data.GetKey("playerData"))
var playerData = JSON.parse(Data.GetKey("playerData") || "[]")
var index = playerData.findIndex(playerData => playerData.name == e.player.name)
if (index == -1) playerData.push(
{ name: e.player.name, earthLocation: { x: 0, z: 0 }, planet: "Earth", isTravelling: false }
Expand Down

0 comments on commit 3ac389c

Please sign in to comment.