Пример #1
0
func bot(client *mcclient.Client) {
	for {
		time.Sleep(time.Second * 5)

		p, ok := findNearestTree(client)
		if !ok {
			return
		}

		ansi.Printf(ansi.Green, "Found tree: %d, %d, %d\n", p.x, p.y, p.z)

		chop(client, p)
	}
}
Пример #2
0
func moveTo(client *mcclient.Client, p xyz) {
	p.x++ // Dont stand in the tree!

	ticker := time.NewTicker(mcclient.Tick)

	ansi.Printf(ansi.Green, "Moving from (%d, %d, %d) to (%d, %d, %d)\n", int(client.PlayerX), int(client.PlayerY), int(client.PlayerZ), p.x, p.y, p.z)

	for (int(client.PlayerX) != p.x) || (int(client.PlayerY) != p.y) || (int(client.PlayerZ) != p.z) {

		//println(client.PlayerX, client.PlayerY, client.PlayerZ)

		if int(client.PlayerX) < p.x {
			client.PlayerX += 0.2
		} else if int(client.PlayerX) > p.x {
			client.PlayerX -= 0.2
		}

		if int(client.PlayerY) < p.y {
			client.PlayerY += 0.2
		} else if int(client.PlayerY) > p.y {
			client.PlayerY -= 0.2
		}

		if int(client.PlayerZ) < p.z {
			client.PlayerZ += 0.2
		} else if int(client.PlayerZ) > p.z {
			client.PlayerZ -= 0.2
		}

		client.PlayerStance = client.PlayerY + 1.0
		die(client.SendPacket(0x0D, client.PlayerX, client.PlayerY, client.PlayerStance, client.PlayerZ, client.PlayerYaw, client.PlayerPitch, client.PlayerOnGround))

		<-ticker.C
	}

	ansi.Printf(ansi.Green, "Done moving\n")
}
Пример #3
0
func chop(client *mcclient.Client, p xyz) {
	for {
		block, _, _, _, _, _ := client.GetBlock(p.x, p.y, p.z)

		if block == 17 {
			moveTo(client, p)
			ansi.Printf(ansi.Green, "Breaking block at (%d, %d, %d)\n", p.x, p.y, p.z)
			die(client.SendPacket(0x0E, int8(0), int32(p.x), int8(p.y), int32(p.z), int8(5)))
			die(client.SendPacket(0x0E, int8(2), int32(p.x), int8(p.y), int32(p.z), int8(5)))
			time.Sleep(time.Second * 3)
			p.y++

		} else {
			break
		}
	}
}
Пример #4
0
func main() {
	flag.Parse()

	if flag.NArg() < 1 {
		ansi.Fprintf(os.Stderr, ansi.RedBold, "usage: %s [options] <server address>\n", os.Args[0])
		os.Exit(2)
	}

	addr := flag.Arg(0)
	username := *usernameP
	password := *passwordP
	debug := *debugP

	var debugWriter io.Writer
	var client *mcclient.Client
	var err error

	if debug {
		debugWriter = DebugWriter{}
	}

	if password == "" {
		if username == "" {
			username = "******"
		}

		ansi.Printf(ansi.Green, "Authenticating offline as %s...\n", username)
		client = mcclient.LoginOffline(username, debugWriter)

	} else {
		ansi.Printf(ansi.Green, "Authenticating as %s...\n", username)
		client, err = mcclient.Login(username, password, debugWriter)
		die(err)
	}

	client.StoreWorld = true
	client.HandleMessage = func(msg string) {
		matches := WhisperRegexp.FindStringSubmatch(msg)
		if matches != nil {
			ansi.Printf(ansi.YellowBold, "Message from %s: %s\n", matches[1], matches[2])
			client.Chat(fmt.Sprintf("/tell %s %s", matches[1], matches[2]))
		}

		//fmt.Printf("# %s\n", mcclient.ANSIEscapes(msg))
		//fmt.Printf("# %s\n", msg)
	}

	go func() {
		/*
			for err := range client.ErrChan {
				ansi.Printf(ansi.RedBold, "Error: %s\n", err.Error())
			}
		*/

		die(<-client.ErrChan)
	}()

	ansi.Printf(ansi.Green, "Connecting to %s...\n", addr)
	die(client.Join(addr))

	ansi.Printf(ansi.Green, "Connected!\n")

	go bot(client)

	kickMessage := client.Run()
	ansi.Printf(ansi.Green, "Disconnected: %s\n", kickMessage)
}
Пример #5
0
func findNearestTree(client *mcclient.Client) (p xyz, ok bool) {
	p = xyz{int(client.PlayerX), int(client.PlayerY), int(client.PlayerZ)}
	radius := 1
	x := -radius
	z := -radius
	p.x -= radius
	p.z -= radius
	dir := 1

mainloop:
	for {
		//fmt.Scanln()

		block, _, _, _, _, ok := client.GetBlock(p.x, p.y, p.z)
		if !ok {
			ansi.Printf(ansi.RedBold, "No log found!\n")
			return p, false
		}

		//println(p.x, p.y, p.z, block)

		if block == 0 {
			under, _, _, _, _, _ := client.GetBlock(p.x, p.y-1, p.z)

			if under == 0 {
				p.y--

			} else {
				if (dir > 0 && x == radius) || (dir < 0 && x == -radius) {
					if (dir > 0 && z == radius) || (dir < 0 && z == -radius) {
						if dir < 0 {
							radius++
							x = -radius
							z = -radius

							p.x -= 1
							p.z -= 1
						}

						dir = -dir

					} else {
						z += dir
						p.z += dir
					}

				} else {
					x += dir
					p.x += dir
				}
			}

		} else if block == 17 {
			// We found log!

			n := p

			for {
				n.y++

				block, _, _, _, _, ok = client.GetBlock(n.x, n.y, n.z)

				if block == 17 { // Log
					continue

				} else if block == 18 { // Leaves, always found above a tree
					break

				} else { // Not a log
					continue mainloop
				}
			}

			for {
				block, _, _, _, _, ok = client.GetBlock(p.x, p.y-1, p.z)

				if block == 17 {
					p.y--
					continue

				} else { // Bottom block of trunk
					return p, true
				}
			}

		} else {
			p.y++
		}
	}

	return p, false
}
Пример #6
0
func main() {
	p := argparse.New("A SPARQL query & update client")
	p.Argument("Endpoint", 1, argparse.Store, "endpoint_uri", "The SPARQL endpoint URI. It is used for all query operations, and update operations when -u is not specified.")
	p.Option('u', "update-endpoint", "UpdateEndpoint", 1, argparse.Store, "URI", "An alternative endpoint URI that is only used for SPARQL update operations. Default: use the query endpoint URI.")
	p.Option('f', "fuseki", "UseFuseki", 0, argparse.StoreConst(true), "", "Interpret endpoint_uri as the URI of a Fuseki dataset, and then use its query and update services as the corresponding endpoints for the session.")
	p.Option('d', "debug", "Debug", 0, argparse.StoreConst(true), "", "Show debug info.")

	args := &Args{}
	err := p.Parse(args)

	if err != nil {
		if cmdLineErr, ok := err.(argparse.CommandLineError); ok {
			ansi.Fprintln(os.Stderr, ansi.RedBold, string(cmdLineErr))
			p.Help()
			os.Exit(2)

		} else {
			die(err, true)
		}
	}

	var queryService, updateService sparql.SparqlService

	if args.UseFuseki {
		dataset := fuseki.NewDataset(args.Endpoint)
		queryService = dataset.QueryService()
		updateService = dataset.UpdateService()

	} else {
		queryService = sparql.NewSparqlService(args.Endpoint)

		if args.UpdateEndpoint != "" {
			updateService = sparql.NewSparqlService(args.UpdateEndpoint)

		} else {
			updateService = queryService
		}
	}

	queryService.Debug = args.Debug
	updateService.Debug = args.Debug

	stdinReader := bufio.NewReader(os.Stdin)
	prefixes := make(map[string]string) // Prefix -> Base URI
	format := argo.Formats["rdfxml"]

mainloop:
	for {
		fmt.Print("> ")

		line, err := stdinReader.ReadString('\n')
		if err == io.EOF {
			return
		}

		if die(err, false) {
			continue mainloop
		}

		line = trimPrefixes(line[:len(line)-1], prefixes)
		line = strings.Trim(line, " \r\n\t")

		if line == "" {
			continue mainloop
		}

		verb := line
		spacePos := strings.IndexRune(line, ' ')
		if spacePos >= 0 {
			verb = line[:spacePos]
		}

		switch strings.ToUpper(verb) {
		case "SELECT":
			rp, err := queryService.Select(line)
			if die(err, false) {
				continue mainloop
			}

			vars := rp.Vars()

			var table Table
			table.SetHeader(vars...)

			for result := range rp.ResultChan() {
				fields := make([]string, len(vars))

				for i, v := range vars {
					fields[i] = result[v].String()
				}

				table.AddRow(fields...)
			}

			ansi.AttrOn(ansi.Yellow)
			table.Print()
			ansi.AttrOff(ansi.Yellow)

		case "ASK":
			result, err := queryService.Ask(line)
			if die(err, false) {
				continue mainloop
			}

			ansi.Printf(ansi.Magenta, "Result: %t\n", result)

		case "CONSTRUCT", "DESCRIBE":
			graph, err := queryService.Graph(line)
			if die(err, false) {
				continue mainloop
			}

			updateRev(graph.Prefixes, prefixes)

			ansi.AttrOn(ansi.Cyan)
			graph.Serialize(format.Serializer, os.Stdout)
			ansi.AttrOff(ansi.Cyan)

		case "INSERT", "DELETE", "LOAD", "CLEAR", "CREATE", "DROP", "COPY", "MOVE", "ADD":
			err := updateService.Update(line)
			if die(err, false) {
				continue mainloop
			}

			ansi.Println(ansi.GreenBold, "OK")

		case "FORMAT":
			formatName := strings.ToLower(line[spacePos+1:])
			newFormat, ok := argo.Formats[formatName]
			if !ok {
				ansi.Fprintf(os.Stderr, ansi.RedBold, "Invalid format: %s\n", formatName)
			}

			format = newFormat

		default:
			ansi.Fprintf(os.Stderr, ansi.RedBold, "Invalid command: %s\n", verb)
		}
	}
}